Python 基本数据类型(三): 字节类型

版权声明:转载请注明来源及作者,谢谢! https://blog.csdn.net/qq_42442369/article/details/84326503

content

  • 字节的创建
  • 字节的操作
  • 字节的相关方法
1. 字节的创建
# 语法:字节名=b"多个单字节" 加b前缀
b=b"abc"
print(b,type(b))
print(len(b))
# b'abc' <class 'bytes'>
# 3
strnull="" # 空字符串
bytesnull=b"" # 空字节
2. 字节的操作
(1) 运算符
# +  *  in   not in  is  is not  ==  <  >
"""
print(b"abc"+b"def")
a=b"abc"
b=b"abc"
print(a is b)
print(b"b" in b)
# b'abcdef'
# True
# True
(2) 索引
a=b"abcdefg"
print(a[0])  # 索引得到的是ascii码
# print(a[100]) # index out of range
for i in a:
    print(i)
"""
97
97
98
99
100
101
102
103
"""
(3) 切片
a=b"abcdefg"
print(a[1:5])
# b'bcde'
3. 字节的相关方法
# 同字符串

猜你喜欢

转载自blog.csdn.net/qq_42442369/article/details/84326503