3. String & Unicode
2016-10-14 本文已影响0人
捕鲸小能手
String
# Unicode string
print('hello, 世界')
print('\u4e2d\u6587')
# Char -> Code point
ord('中')
# Code point -> Char
chr(65)
# Bytes
print(b'hello')
# String -> Bytes
'hello'.encode('utf-8')
# Bytes -> String
b'hello'.decode('utf-8')
# String length
len('中文') # 2
# String bytes
len('中文'.encode('utf-8')) # 6
String format
print('%s, %d, %f, %x' % ('hello, world', 2333, 3.14, 16))