# Python 中的字符串类型主要有两种:str 和 bytes
# 1. str 类型(Unicode 字符串)
# str 是 Python 中最常用的字符串类型,支持 Unicode 编码。
# 示例:
s = "Hello, 世界"
print(type(s)) # 输出: <class 'str'>
print(s) # 输出: Hello, 世界
# 2. bytes 类型(字节字符串)
# bytes 类型表示不可变的字节序列,通常用于处理二进制数据或编码后的文本。
# 示例:
b = b"Hello, world"
print(type(b)) # 输出: <class 'bytes'>
print(b) # 输出: b'Hello, world'
# 注意:str 和 bytes 之间可以互相转换
# 将 str 转换为 bytes
byte_string = s.encode('utf-8')
print(byte_string) # 输出: b'Hello, \xe4\xb8\x96\xe7\x95\x8c'
# 将 bytes 转换为 str
string_from_bytes = byte_string.decode('utf-8')
print(string_from_bytes) # 输出: Hello, 世界
上一篇:python解析json字符串
下一篇:python字符转换数字
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站