Python中字符串格式化的细节

Posted 努力的生活

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python中字符串格式化的细节相关的知识,希望对你有一定的参考价值。

 1 >>> print("%.2f %s are worth $%d"%(4.5560,"Argentine Pesos",1))
 2 4.56 Argentine Pesos are worth $1
 3 >>> template = "%.2f %s are worth $%d"
 4 >>> templat % (4.5560,"Argentine Pesos",1)
 5 Traceback (most recent call last):
 6   File "<pyshell#230>", line 1, in <module>
 7     templat % (4.5560,"Argentine Pesos",1)
 8 NameError: name ‘templat‘ is not defined
 9 >>> template = "%.2f %s are worth $%d"
10 >>> template % (4.5560,"Argentine Pesos",1)
11 4.56 Argentine Pesos are worth $1

Python3中,一段代码压缩成一物理行print可直接生成输出结果,如line1,line2。

物理行分解,在字符串格式定义好之后不使用print也可输出结果。

以上是关于Python中字符串格式化的细节的主要内容,如果未能解决你的问题,请参考以下文章

Python中字符串格式化的细节

如何从我的 DatePicker 片段中传输格式化的日期字符串?

python中一些简单的代码优化细节

在 Python 格式(f-string)字符串中,!r 是啥意思? [复制]

我应该如何使用 Outlook 发送代码片段?

笨方法学Python摘记