关于转义字符的理解
Topic source修改版:
只要存储或者打印的是字符串,就必须要加上( ' ' ) 去表明这一段是字符串。
如果字符串里面有 单引号( ' )或者反斜杠( \ )这类字符需要打印出来,需要用转义字符(\)去指明;
如果字符串要打印那些有本身具有转义功能的字符,就要加上(r)在( ' ' )之前。
示例,要输出 hello,my name's "张三"
print('hello,my name \' s "张三" ') 成功
print(r'hello,my name \' s "张三" ') 成功
print('hello,my name ' s "张三" ') 失败
print(r'hello,my name ' s "张三" ') 失败
即 双引号(" ")可以被打印为单纯的字符
单引号( ' )不可被打印为单纯的字符,( ' )必须借助转义字符( \ )的帮助。
r 只对制表符中的 \ , \n , \t 此类转义字符有效,将它们打印为单纯的字符,消除它们的本身的功能。
- 1
L
只要存储或者打印的是字符串,就必须要加上( ' ' ) 去表明这一段是字符串,
如果字符串里面有 单引号( ' )或者反斜杠( \ )这类字符,需要用转义字符(\)去指明,如果字符串里面有不转义的字符,就要加上(r)在( ' ' )之前。
经验证,要输出 hello,my name's "张三"
print('hello,my name \' s "张三" ') 成功
print(r'hello,my name \' s "张三" ') 成功
print('hello,my name ' s "张三" ') 失败
print(r'hello,my name ' s "张三" ') 失败
即 r (" ")可以被视作单纯的字符
( ' )不可被视作为单纯的字符,( ' )仍需借助转义字符(\)的帮助。
而 r 只对制表符中的 \ , \n , \t 此类转义字符有效,将它们视作单纯的字符,消除他们的本身的功能。