Python bytearray/bytes/string区别 - Python零基础入门教程

目录

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

一.字节与字符的区别

在讲解 bytearray / bytes / **string **三者的区别之前,有必要来了解一下字节和字符的区别;

1.字节概念

字节(Byte )是计算机信息技术用于计量存储容量的一种计量单位,作为一个单位来处理的一个二进制数字串,是构成信息的一个小单位。最常用的字节是八位的字节,即它包含八位的二进制数;

  • 位 ( bit ) 是计算机 内部数据 储存的最小单位,11001100 是一个八位二进制数;

  • ** 字节 ( byte ) 是计算机中 数据处理 的基本单位,习惯上用大写 B 来表示, 1B ( byte , 字节 ) = 8 bit ( 位 ) ;**

    1 KB = 1024 B(字节);
    1 MB = 1024 KB; (2^10 B)
    1 GB = 1024 MB; (2^20 B)
    1 TB = 1024 GB; (2^30 B)

2.字符概念

字符 是指计算机中使用的字母、数字、字和符号,包括:1、2、3、A、B、C、~!·#¥%……—*()——+等等;

  • 一般 utf-8 编码下,一个汉字 字符 占用 3 个 字节;
  • 一般 gbk 编码下,一个汉字 字符 占用 2 个 字节;

3.字符串概念

字符串是字符序列,它是一种抽象的概念,不能直接存储在硬盘 – 字节串是给计算机看的,给计算机传输或者保存的,Python 中,程序中的文本都用字符串表示

4.字节串概念

字节串是字节序列,它可以直接存储在硬盘, 字节串是给计算机看的。它们之间的映射被称为编码 / 解码 – 字符串是给人看的,用来操作的;

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

"""


if __name__ == "__main__":

    # 字符串str 转 字节bytes
    s = '猿说python'
    b = s.encode()  # 编码,默认的是UTF-8
    print(b)
    print(type(b))

    # 字节bytes 转 字符串str
    b = b'\xe7\x8c\xbf\xe8\xaf\xb4python'.decode(encoding='UTF-8')  # 解码
    print(b)
    print(type(b))

'''
输出结果:

b'\xe7\x8c\xbf\xe8\xaf\xb4python'
<class 'bytes'>
猿说python
<class 'str'>
'''

二.str / bytes / bytearray 区别

1.str 是字符数据(如:文本,给人看的),bytes 和 bytearray 是字节数据(如:二进制数据,给计算机看的),它们都是序列,可以进行迭代遍历。

2.str 和 bytes 是不可变序列,通过 str 类型的通用函数,比如 find 、replace 、islower 等函数修改后实际上是重新创建了新对象;bytearray 是可变序列,可以原处修改字节。

3.bytes 和 bytearray 都能使用 str 类型的通用函数,比如 find 、replace 、islower 等,不能用的是 str 的格式化操作。

4.Python 3.x 中默认 str 是 unicode 格式编码的,例如 UTF-8 字符集。

三.string 与 bytes / bytearray 相互转换

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

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

"""

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

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

'''
输出结果:

b'https://www.codersrc.com'
<class 'bytes'>
b'https://www.codersrc.com'
<class 'bytes'>
'''

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

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

"""

if __name__ == "__main__":
    # 字节对象b
    b = bytes("python教程-猿说python","utf-8")

    #方案一:
    s2 = bytes.decode(b)
    # 方案二:
    s3 = b.decode()

    print(s2)
    print(s3)

'''
输出结果:

python教程-猿说python
python教程-猿说python
'''

注意:如果 bytes 初始化含有中文的字符串必须设置编码格式,否则报错:TypeError: string argument without an encoding

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

"""

b = bytes("猿说python")

>>> b = bytes("猿说python")
>>> TypeError: string argument without an encoding

四.猜你喜欢

  1. Python for 循环
  2. Python 字符串
  3. Python 列表 list
  4. Python 元组 tuple
  5. Python 字典 dict
  6. Python 条件推导式
  7. Python 列表推导式
  8. Python 字典推导式
  9. Python 函数声明和调用
  10. Python 不定长参数 *argc/**kargcs
  11. Python 匿名函数 lambda
  12. Python return 逻辑判断表达式
  13. Python 字符串/列表/元组/字典之间的相互转换
  14. Python 局部变量和全局变量
  15. Python type 函数和 isinstance 函数区别
  16. Python is 和 == 区别
  17. Python 可变数据类型和不可变数据类型
  18. Python 浅拷贝和深拷贝

未经允许不得转载:猿说编程 » Python bytearray/bytes/string 区别

本文由博客 - 猿说编程 猿说编程 发布!

posted @ 2021-06-21 11:05  猿说编程  阅读(264)  评论(0编辑  收藏  举报