北京网帮你
python字符串的运算与字节数组的操作方法
时间:2019-09-11 00:50:44 浏览:148

字符串的运算:
 +  +=  *  *=
 <  <=  >  >=  ==  !=
 in / not in
 索引和切片
函数:
 len(x)
 max(x)
 min(x)
 sum(x)
 any(x)
 all(x)


bytes 与 str 的区别:
 bytes 存储字节(0~255)
 str 存储 unicode 字符(0~65535或更大)

str 与 bytes转换
       编码(encode)
   str ----------> bytes
      b = s.encode(encoding='utf-8')

       解码(decode)
   bytes -------------> str
      s = b.decode(encoding='utf-8')



字节数组 bytearray
 可变的字节序列

创建函数bytearray
  bytearray()           创建字节数组
  bytearray(可迭代对象)   同bytes(可迭代对象)
  bytearray(整数n)     ...
  bytearray(字符串, encoding='utf-8')

运算操作:
 +  +=  *  *=
 比较运算: < <= > >= == !=
 in / not in
 索引 index / 切片 slice
 (字节数组支持索引和切片赋值,规则同列表的索引和切片赋值规则)

字节数组的方法:
 详见:
   help(bytearray)


[上一篇]python文件的操作流程说明
[下一篇]python字节串 bytes作用与说明
Copyright 2022 © 93580.com.cn 网帮你

2022 © 版权所有 红花岗区网帮你信息技术工作室

工信部备案号:黔ICP备2024036985号-2

请使用网帮你微信扫码登录