python 字符串前加r和f
字符串前加 f 的含義
-
- 看例子
- 用法
- 總結
?
官方文檔:點擊這里
如果你今天將就而選擇參考了我的文檔,總有一天你還是會去閱讀官方文檔。
先看例子
list_ = [1,2,3] print(list_, f'has a length of {len(list_)}.')# [1,2,3] has a length of 3.- 1
- 2
- 3
- 4
看懂這個例子,用法也基本掌握了。
用法
官方文檔:
Format strings contain “replacement fields” surrounded by curly braces {}. Anything that is not contained in braces is considered literal text, which is copied unchanged to the output. If you need to include a brace character in the literal text, it can be escaped by doubling: {{ and }}.
大概意思就是:
格式化 {} 內容,不在 {} 內的照常展示輸出,如果你想輸出 {},那就用雙層 {{}} 將想輸出的內容包起來。
效果如下:
list_ = [1,2,3] print(list_, f'has a length of {len(list_)}.') # [1,2,3] has a length of 3.print(list_, f'has a length of {{len(list_)}}.') # [1,2,3] has a length of {len(list_)}.print(list_, f'has a length of {{{len(list_)}}}.') # [1,2,3] has a length of {3}.- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
總結
f-string:?formatted string literals, 格式化字符串常量。
功能同str.format() %-formatting,
較兩者更簡潔易用,推薦使用
需要注意的是,Python3.6及以后的版本可用
?
?
字符串前加 r?的含義
print (glob.glob(r"/home/qiaoyunhao/*/*.png"),"\n")#加上r讓字符串不轉義
總結
以上是生活随笔為你收集整理的python 字符串前加r和f的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Python 将两层列表展开平铺成一层
- 下一篇: Python 图片挑选程序,tkinte