python使用f格式化字符串

390次阅读
没有评论

python使用f格式化字符串

1、若要格式化字符串,可在字符串开始引号前添加f或F。

通过这种方式,我们可以直接中引入变量值,只需将变量放在{ 和 }之间。

>>> year = 2016
>>> event = 'Referendum'
>>> f'Results of the {year} {event}'
'Results of the 2016 Referendum'

2、将Python变量放入{}外,还可将函数放入{}:

>>> import math
>>> print(f'The value of pi is approximately {math.pi:.3f}.')
The value of pi is approximately 3.142.
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试

相关文章:

版权声明:wuyou2021-04-26发表,共计309字。
新手QQ群:570568346,欢迎进群讨论 Python51学习