🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
Formatted string literals ================= Python3.6 新增 语法格式如下: ~~~ f_string ::= (literal_char | "{{" | "}}" | replacement_field)* replacement_field ::= "{" f_expression ["!" conversion] [":" format_spec] "}" f_expression ::= (conditional_expression | "*" or_expr) ("," conditional_expression | "," "*" or_expr)* [","] | yield_expression conversion ::= "s" | "r" | "a" format_spec ::= (literal_char | NULL | replacement_field)* literal_char ::= <any code point except "{", "}" or NULL> ~~~ 示例: ~~~ name = 'stephen' age = 30 height = 1.83 me = {'name':'stephen', 'age':30, 'height':1.83} f'My name\'s {name}, {age} years old and {height}m height.' # "My name's stephen, 30 years old and 1.83m height." f'--{}--' # SyntaxError: f-string: empty expression not allowed # 配合Format Specification Mini-Language f'--{name:.3s}--{age:*^6d}--{height:e}--' # '--ste--**30**--1.830000e+00--' # 允许嵌套字段 f'--{name:.{height:.0f}s}--{height:.0f}--' # '--st--2--' ~~~