Skip to content

Latest commit

 

History

History
53 lines (38 loc) · 971 Bytes

c01_04.md

File metadata and controls

53 lines (38 loc) · 971 Bytes

1.4 反斜杠的倔强: 不写最后

` 在 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