1. 长字符串
如果需要写一个非常非常长的字符串,它需要跨多行,那么,可以使用三个引号代替普通
引号
>>> print '''aaaaa
... bbbbbbbbbbb
... ccccccccccc'''
aaaaa
bbbbbbbbbbb
ccccccccccc
>>>
也可以使用三个双引号,如"""Like This"""
2. 原始字符串
原始字符串对于反斜线的使用并不会过分挑剔。
在普通字符串中,反斜线有特殊的作用:它会转义,长字符串都是普通字符串。
但是有些情况下,我们并不需要转义作用
>>> path='c:\nowhere'
>>> path
'c:\nowhere'
>>> print path
c:
owhere
>>> print 'c:\\nowhere'
c:\nowhere
>>> print r'c:\nowhere'
c:\nowhere
>>>
可以看到,原始字符串,以r开头。
不能在原始字符串结尾输入反斜线,除非对反斜线进行转义。
>>> print r'c:\nowhere\'
File "<stdin>", line 1
print r'c:\nowhere\'
^
SyntaxError: EOL while scanning string literal
>>> print r'c:\nowhere' '\\'
c:\nowhere\
>>>
3. Unicode字符串
Python中的普通字符串在内部是以8位ASCII码形成存储的,而Unicode字符串
则存储为16位Unicode字符,这样就能够表示更多的字符集了
>>> u'Hello,world!'
u'Hello,world!'
>>>
在Python3.0中,所有字符串都是Unicode字符串。