python字符串str和字节数组相互转化

简介: b = b"Hello, world!" # bytes object s = "Hello, world!" # str object print('str --> bytes') print(bytes(s, encoding="utf8")) print(str.

 

b = b"Hello, world!"  # bytes object  
s = "Hello, world!"   # str object 

print('str --> bytes')
print(bytes(s, encoding="utf8"))    
print(str.encode(s))   # 默认 encoding="utf-8"
print(s.encode())      # 默认 encoding="utf-8"

print('\nbytes --> str')
print(str(b, encoding="utf-8"))   
print(bytes.decode(b))  # 默认 encoding="utf-8"
print(b.decode())       # 默认 encoding="utf-8"

 

目录
相关文章
|
9天前
|
Python
1167: 分离字符串(PYTHON)
1167: 分离字符串(PYTHON)
|
27天前
|
大数据 Python
使用Python查找字符串中包含的多个元素
本文介绍了Python中查找字符串子串的方法,从基础的`in`关键字到使用循环和条件判断处理多个子串,再到利用正则表达式`re模块`进行复杂模式匹配。文中通过实例展示了如何提取用户信息字符串中的用户名、邮箱和电话号码,并提出了优化策略,如预编译正则表达式和使用生成器处理大数据。
20 1
|
1月前
|
数据挖掘 开发者 Python
Python:字符串判断子串
Python:字符串判断子串
|
1月前
|
程序员 数据安全/隐私保护 Python
Python:翻转字符串
Python:翻转字符串
|
1月前
|
Python
利用Python生成字符串连接
利用Python生成字符串连接
19 0
|
1月前
|
索引 Python
Python系列(14)—— 字符串运算符
Python系列(14)—— 字符串运算符
|
1月前
|
存储 自然语言处理 数据挖掘
Python:计算字符串中每个单词出现的次数
Python:计算字符串中每个单词出现的次数
|
1天前
|
数据采集 Python
python学习9-字符串
python学习9-字符串
|
9天前
|
Python
171: 字符串的倒序(python)
171: 字符串的倒序(python)
|
26天前
|
JSON C++ 数据格式
【Python 基础教程 08】全面入门到精通:Python3 字符串操作实战教程与深度指南
【Python 基础教程 08】全面入门到精通:Python3 字符串操作实战教程与深度指南
82 0