Python str / bytes / unicode 区别详解

ChatGPT 3.5 国内中文镜像站免费使用啦

零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门


一.前言

        在讲解 str / bytes / unicode 区别之前首先要明白字节和字符的区别,请参考:bytearray / bytes / string 区别 中对字节和字符有清晰的讲解,最重要是明白:

  •         字符 str 是给人看的,例如:文本保存的内容,用来操作的;
  •         字节 bytes 是给计算机看的,例如:二进制数据,给计算机传输或者保存的;

二.Python str / bytes / unicode 区别


1.Python2.x 版本中 str / bytes / unicode 区别

        Python2.x 版本中 str 跟 bytes 是等价的;值得注意的是:bytes 跟 unicode 是等价的,详情见下图

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python str / bytes / unicode 区别详解.py
@Time:2021/05/09 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
 
"""


s1 = u"Hello, World!"
s2 = "Hello, World!"
print(type(s1))
print(type(s2))


'''
输出:

<type 'unicode'>
<type 'str'>
'''

2.Python3.x 版本中 str / bytes / unicode 区别

        Python3.x 版本中 str 跟 unicode 是等价的;值得注意的是:bytes 跟 unicode 是不等价的,详情见下图

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python str / bytes / unicode 区别详解.py
@Time:2021/05/09 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
 
"""

s1 = u"Hello, World!"
s2 = "Hello, World!"
print(type(s1))
print(type(s2))


'''
输出:

<class 'str'>
<class 'str'>
'''

三.Python string 与 bytes 相互转换


1.string 经过编码 encode 转化成 bytes

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python str / bytes / unicode 区别详解.py
@Time:2021/05/09 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
 
"""


s = "www.codersrc.com"
#将字符串转换为字节对象
b2 = bytes(s,encoding='utf8') #必须制定编码格式
# print(b2)

#方法一:字符串encode将获得一个bytes对象
b3 = str.encode(s)
#方法二:字符串encode将获得一个bytes对象
b4 = s.encode()
print(b3)
print(type(b3))
print(b4)
print(type(b4))

'''
输出结果:

b'www.codersrc.com'
<class 'bytes'>
b'www.codersrc.com'
<class 'bytes'>
'''

2. bytes 经过解码 decode 转化成 string

# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:猿说编程
@Blog(个人博客地址): www.codersrc.com
@File:Python str / bytes / unicode 区别详解.py
@Time:2021/05/09 07:37
@Motto:不积跬步无以至千里,不积小流无以成江海,程序人生的精彩需要坚持不懈地积累!
 
"""


# 字节对象b2
    # 如果含有中文,必须制定编码格式,否则报错TypeError: string argument without an encoding
    b2 = bytes("猿说python", encoding='utf8')

    # 方法二:bytes对象decode将获得一个字符串
    s2 = bytes.decode(b2)
    # 方法二:bytes对象decode将获得一个字符串
    s3 = b2.decode()
    print(s2)
    print(s3)

'''
输出结果:

猿说python
猿说python
'''

四.猜你喜欢

  1. Python 条件推导式
  2. Python 列表推导式
  3. Python 字典推导式
  4. Python 不定长参数 *argc/**kargcs
  5. Python 匿名函数 lambda
  6. Python return 逻辑判断表达式
  7. Python is 和 == 区别
  8. Python 可变数据类型和不可变数据类型
  9. Python 浅拷贝和深拷贝
  10. Python 异常处理
  11. Python 线程创建和传参
  12. Python 线程互斥锁 Lock
  13. Python 线程时间 Event
  14. Python 线程条件变量 Condition
  15. Python 线程定时器 Timer
  16. Python 线程信号量 Semaphore
  17. Python 线程障碍对象 Barrier
  18. Python 线程队列 Queue – FIFO
  19. Python 线程队列 LifoQueue – LIFO
  20. Python 线程优先队列 PriorityQueue
  21. Python 线程池 ThreadPoolExecutor(一)
  22. Python 线程池 ThreadPoolExecutor(二)
  23. Python 进程 Process 模块
  24. Python 进程 Process 与线程 threading 区别
  25. Python 进程间通信 Queue / Pipe
  26. Python 进程池 multiprocessing.Pool
  27. Python GIL 锁

ChatGPT 3.5 国内中文镜像站免费使用啦
© 版权声明
THE END
喜欢就支持一下吧
点赞2 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容