python中的raw string的使用

背景

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

实例

输入

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

输出

s is: 
fadfafa
hello
s1 is: 
fadfafa
hello
a is: 
fadfafa
hello

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

原文地址:https://www.cnblogs.com/anita-harbour/p/9360205.html