大家好,欢迎来到IT知识分享网。
“\”在 Python 中的用法主要有两种
1、在行尾时,用做续行符
[root@localhost ~]$ cat demo.py
print(“hello “\
“world”)
[root@localhost ~]$
[root@localhost ~]$ python demo.py
hello world
2、在字符串中,用做转义字符,可以将普通字符转化为有特殊含义的字符。
>>> str1=’\nhello’ #换行
>>> print(str1)
hello
>>> str2=’\thello’ #tab
>>> print(str2)
hello
但是如果你用单\结尾是会报语法错误的
>>> str3=”\”
File “<stdin>”, line 1
str3=”\”
^
SyntaxError: EOL while scanning string literal
就算你指定它是个 raw 字符串,也不行。
>>> str3=r”\”
File “<stdin>”, line 1
str3=r”\”
^
SyntaxError: EOL while scanning string literal
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/186444.html