栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

什么是打印(f“…”)

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

什么是打印(f“…”)

f
方法格式化字符串字面量 和它的新功能
Python 3.6


格式的字符串文字F-串
是前缀字符串文字

'f'
'F'
。这些字符串可能包含替换字段,这些替换字段由花括号分隔
{}
。尽管其他字符串文字始终具有恒定值,但是格式化的字符串实际上是在运行时评估的表达式。


格式化字符串文字的一些示例:

>>> name = "Fred">>> f"He said his name is {name}.""He said his name is Fred.">>> name = "Fred">>> f"He said his name is {name!r}.""He said his name is Fred.">>> f"He said his name is {repr(name)}." # repr() is equivalent to !r"He said his name is Fred.">>> width = 10>>> precision = 4>>> value = decimal.Decimal("12.34567")>>> f"result: {value:{width}.{precision}}" # nested fieldsresult: 12.35>>> today = datetime(year=2017, month=1, day=27)>>> f"{today:%B %d, %Y}" # using date format specifierJanuary 27, 2017>>> number = 1024>>> f"{number:#0x}" # using integer format specifier0x400


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/574770.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号