encode

python字符串的encode方法可以对字符串进行编码,编码后返回的是bytes类型数据,encoding参数设置采用哪一种编码,在网络通讯中无法直接使用字符串,必须先进行编码。

方法定义如下

def encode(self, encoding='utf-8', errors='strict'):

默认使用utf-8进行编码

word = 'hello world'
byte_word = word.encode(encoding='utf-8')
print(byte_word)   # b'hello world'
print(type(byte_word))  # <class 'bytes'>

扫描关注, 与我技术互动

QQ交流群: 211426309

加入知识星球, 每天收获更多精彩内容

分享日常研究的python技术和遇到的问题及解决方案