[英]Does Python3 still need raw string in regular expression?
在Python 2中,當使用正則表達式時,我們使用r'expression',在Python 3中是否仍需要在前面加上“ r”,因為我知道Python 3默認使用Unicode
是。 反斜杠轉義序列仍然存在於Python 3字符串中,因此,以r
開頭的原始字符串會有所不同,如以下簡單示例所示:
>>> s = 'hello\n'
>>> raw = r'hello\n'
>>> s
hello\n
>>> raw
hello\\n
>>> print(s)
hello
>>> print(raw)
hello\n
原始字符串對於編寫\\等字符而無需轉義仍然有用。 通常在正則表達式和窗口路徑等中有用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.