f-string
f-string
python
3.6 引入了一种新的字符串常量:f-tring
或者叫格式化字符串
f-string
使用f作为前缀,和str.format()
非常相似,它使用花括号包含的占位符。占位符是一个在运行时计算表达式,然后使用format
协议格式化
>>> name = "Fred"
>>> f"He said his name is {name}."
'He said his name is Fred.'
>>> width = 10
>>> precision = 4
>>> value = https://www.it610.com/article/decimal.Decimal("12.34567")
>>> f"result: {value:{width}.{precision}}"# nested fields
'result:12.35'
性能
下图是各种字符串格式化方法的执行时间
![f-string](https://img.it610.com/image/info10/7bedfc2d981e4c96b1734b1aeff24d78.jpg)
文章图片
image 参考
https://cito.github.io/blog/f-strings/
【f-string】https://docs.python.org/3/whatsnew/3.6.html
推荐阅读
- python学习之|python学习之 实现QQ自动发送消息
- 逻辑回归的理解与python示例
- python自定义封装带颜色的logging模块
- 【Leetcode/Python】001-Two|【Leetcode/Python】001-Two Sum
- Python基础|Python基础 - 练习1
- Python爬虫|Python爬虫 --- 1.4 正则表达式(re库)
- Python(pathlib模块)
- python青少年编程比赛_第十一届蓝桥杯大赛青少年创意编程组比赛细则
- Python数据分析(一)(Matplotlib使用)
- Python|Python 9.20