python3.6新特性 f-strings

格式化的字符串文字以前缀'f'为的格式字符串为前缀,并且与之接受的格式字符串类似str.format()。它们包含由花括号包围的替换字段。替换字段是表达式,在运行时进行评估,然后使用format()协议进行格式化 :

>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'
>>> width = 10
>>> precision = 4
>>> value = decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}"  # nested fields
'result:      12.35'
发布了73 篇原创文章 · 获赞 41 · 访问量 15万+

猜你喜欢

转载自blog.csdn.net/weixin_37989267/article/details/90211204
今日推荐