Python——format()/str.format()函数

格式化输出,除了类似于C语言的格式化输出外,还有str.format()方法,Python内建的format()函数,允许用户将待输出值以参数的形式,调用format()函数,在Python交互式shell下,通过 help(format) 命令可以获取详细信息:

因此,若value是str类型的变量,则 format(value,format_spec)  <==>  value.format(format_spec) 

当value不是str类型,例如 type(value) == int 时, value.format(format_spec) 就会报错:

 但是 format(value,format_spec) 可以运行:

原文地址:https://www.cnblogs.com/oddcat/p/9648590.html