'xe5xadx97xe7xacxa6xe4xb8xb2xe5xadxa6xe4xb9xa0'
>>>
>>> str.decode('utf-8') #解码过程,将utf-8解码为unicode
u'u5b57u7b26u4e32u5b66u4e60'
>>> str.decode('utf-8').encode('gbk') #编码过程,将unicode编码为gbk
'xd7xd6xb7xfbxb4xaexd1xa7xcfxb0'
>>> str.decode('utf-8').encode('utf-8') #将unicode编码为utf-8
'xe5xadx97xe7xacxa6xe4xb8xb2xe5xadxa6xe4xb9xa0'
字符串分割变换
>>> str='Learn string'
>>> '-'.join(str)
'L-e-a-r-n- -s-t-r-i-n-g'
>>> l1=['Learn','string']
>>> '-'.join(l1)
'Learn-string'
>>>
>>> str.split('n')
['Lear', ' stri', 'g']
>>> str.split('n',1)
['Lear', ' string']
>>> str.rsplit('n',1)
['Learn stri', 'g']
>>>
>>> str.splitlines()
['Learn string']
>>>
>>> str.partition('n')
('Lear', 'n', ' string')
>>> str.rpartition('n')
('Learn stri', 'n', 'g')
string模块源代码
"""A collection of string operations (most are no longer used).
Warning: most of the code you see here isn't normally used nowadays.
Beginning with Python 1.6, many of these functions are implemented as
methods on the standard string object. They used to be implemented by
a built-in module called strop, but strop is now obsolete itself.
Public module variables:
whitespace -- a string containing all characters considered whitespace