Python笔记|字符串的转义
重新梳理一遍python的基础知识
- 除了数字,Python 还可以操作字符串。字符串有多种表现形式,用单引号(
'……'
)或双引号("……"
)标注的结果相同 。反斜杠\
用于转义:
>>>'spam eggs'
# 直接输出 ‘spam eggs’
>>> "doesn't" *
# 使用双引号直接输出"doesn’t"
>>> 'doesn\'t'
# 使用\'
转义单引号
"doesn't"
- 不需要在单引号里转义双引号
"
,但需要转义字符串里的单引号\'
。
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
反过来,也不需要在双引号里转义单引号 '
,但需要转义字符串里的双引号 \"
。
>>>"\"Yes,\" they said."
'"Yes," they said.&