https://docs.python.org/zh-cn/3/tutorial/inputoutput.html#tut-f-strings
https://docs.python.org/zh-cn/3/library/string.html#format-string-syntax
f-string
f-string
是标注了 f
或 F
前缀的字符串字面值。
这种字符串可包含替换字段,即以 {表达式}
标注的方式,将 Python 表达式的值添加到字符串内。
常用代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
format()
方法{}
中表达式语法与 f-string
类似,不再介绍
常用代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
运行输出:
1 2 3 4 5 6 7 8 9 |
|
%
运算符%
运算符(求余符)也可用于字符串格式化。
已经不推荐使用:
https://docs.python.org/zh-cn/3/tutorial/inputoutput.html#old-string-formatting
=== 全文完 ===