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

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

 

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

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

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

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

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

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

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-07-21
  • 2022-12-23
  • 2022-12-23
  • 2021-09-08
  • 2021-08-30
  • 2021-06-29
猜你喜欢
  • 2021-09-19
  • 2021-07-04
  • 2022-12-23
  • 2022-12-23
  • 2021-06-21
  • 2021-11-28
  • 2022-12-23
相关资源
相似解决方案