python中的raw string的使用

背景

我们经常需要使用raw string,在应用过程中,比如要使字符串中带一些转义字符或者其他的一些符号,我们就需要保持我们的字符成为raw string.

实例

输入

s = 'fadfafa\nhello'
s1 = r'fadfafa\nhello'
print("s is: ", s, sep='\n')
print("s1 is: ", s1, sep='\n')
a = repr(s)[1:-1]
print("a is: ", a, sep='\n')

输出

s is: 
fadfafa
hello
s1 is: 
fadfafa\nhello
a is: 
fadfafa\nhello

通过上面的实例我们可以知道,我们想要获得raw string 可以通过在字符串前面加入r来装换,如果是字符串变量的话,我们可以通过repr来操作

posted @ 2018-07-24 15:23  Anita_harbour  阅读(595)  评论(0编辑  收藏  举报