In Python 2.x:
<type 'str'>
or'Hello'
is byte string.<type 'unicode'>
oru'Hello'
is unicode string.
# convert byte string to unicode string'Hello'.decode('utf-8')# u'Hello'# convert unicode string to byte stringu'Hello'.encode('utf-8')# 'Hello'u'你好'.encode('utf-8')# '\xe4\xbd\xa0\xe5\xa5\xbd'
In Python 3.x:
<type 'str'>
or'Hello'
is unicode string.<type 'bytes'>
orb'Hello'
is byte string.
# convert bytes to unicodeb'Hello'.decode('utf-8')# 'Hello'# convert unicode to bytes'Hello'.encode('utf-8')# b'Hello''你好'.encode('utf-8')# b'\xe4\xbd\xa0\xe5\xa5\xbd'