桂林市做網(wǎng)站的公司個人博客登錄入口
1、r前綴
一般來說,'\n’是一個換行符,是一個字符串;而加上r為前綴后,不會以任何特殊方式處理反斜杠。因此,r"\n" 是包含 ‘\’ 和 ‘n’ 的雙字符字符串;示例如下:
>>> print(r'\n')
\n
>>> print('\n')>>>
"r"也常用于正則表達式中,\b在正則中代表空字符串
>>> re.findall(r'\bf[a-z]*', 'which foot or hand fell fastest')
['foot', 'fell', 'fastest']
>>> re.findall('\bf[a-z]*', 'which foot or hand fell fastest')
[]
>>>
以"r"為前綴的字符串,表示的是原始字符串。相對于普通字符串,原始字符串中的內(nèi)容會原樣輸出。即使字符串中含有轉(zhuǎn)義字符,如常見的換行符“\n”、縮進符“\t”等,在原始字符串中它們不會進行轉(zhuǎn)義,都會原樣輸出。
詳見正則鏈接
2、b前綴
加上b前綴代表是byte類型,示例如下:
>>> type('sss')
<class 'str'>
>>> type(b'sss')
<class 'bytes'>
>>>
其中有三種情況,如下所示:
- Single quotes: b’still allows embedded “double” quotes’ 即單引號可以包含雙引號
- Double quotes: b"still allows embedded ‘single’ quotes" 即雙引號可以包含單引號
- Triple quoted: b’‘‘3 single quotes’’', b"““3 double quotes””" 即三引號可以包含單引號,也可以包含雙引號
詳見byte鏈接
3、u 前綴
u’'代表的是對字符串進行unicode編碼。python3.0以上的版本,默認的編碼就是UTF-8編碼,而在2.0版本,python使用的是ascii編碼
4、f 前綴
>>> var1='hello world'
>>> print(f'i am lichf {var1}')
i am lichf hello world
>>>
詳見我的另一篇介紹format的文章
詳見f-string鏈接