天天看點

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'