python字符串前加'r'

>>> str1='hello\nworld'
>>> str2=r'hello\nworld!'
>>> print str1
hello
world
>>> print str2
hello\nworld!
>>>

r的作用:保留字符串的原意,对转义符不作处理。

由于正则表达式和 \ 会有冲突,因此,当一个字符串使用了正则表达式后,最好在前面加上'r'。

你可能感兴趣的:(python,字符串,r)