python字符串
python字符串,可以用成对的单引号('......')或双引号("......")来标示(标示类型为字符串)
如果字符串内部存在特殊字符,例如单引号或双引号,遵循以下规则:
1. 如果字符串使用成对的单引号来标示,字符串内部的单引号字符一定要用转义字符,字符串内部的双引号字符可以不用转义字符
2. 如果字符串使用成对的双引号来标示,字符串内部的单引号字符可以不用转义字符,字符串内部的双引号字符一定要用转义字符
示例:
>>> '"Isn\'t," they said.'
'"Isn\'t," they said.'
>>> mystring = '"Isn\'t," they said.'
>>> print(mystring)
"Isn't," they said.
>>> r'"Isn\'t," they said.' //r表示原始字符串,\'使用转义解释器就不会误认为是字符串结束符
'"Isn\\\'t," they said.'
>>> mystring = r'"Isn\'t," they said.'
>>> print (mystring)
"Isn\'t," they said.