天天看点

python中字符串的方法

一.字符串

1.encode()方法作用:将Unicode表示的字符串转化为ascii或者utf-8类型

'still cold'.encode('ascii')
'still cold'.encode('utf-8')
'瑞兹'.encode('utf-8')
#显示为
b'\xe7\x91\x9e\xe5\x85\xb9'
b'still cold'
b'\xe7\x91\x9e\xe5\x85\xb9'
           

2.decode( )方法作用:将bytes类型数据转化为指定类型的字符串

b'The shy'.decode('ascii')
b'Rookie'.decode('utf-8')
结果:
'The shy'
'Rookie'