python字符串和字节串_python字符串和字节系列

一字符串

1.字符串系列操作

2.字符串编码

默认:UTF-8

创建字符串时指定编码方式:str(object='',encoding='utf-8',errors='strict')

bytes对象的对象方法:b.decode(encoding,errors)把字节码b解码

s.encode(encoding='utf-8',errors='strict') 把字符串s编码为字节码对象

3.字符串格式化

%形式:同C语言           例:'姓名:%s, 年龄:%d, 体重:%3.2f' %('张三',20,53)

'姓名:张三, 年龄:20, 体重:53.00'

format内置函数:         format(value,格式)              例: format(81.2,"0.5f")    输出:'81.20000'   (字符串)

字符串的format方法:      str.format(格式字符串, 值1,值2,...)      s.format(值1,值2,...)    s.format_map(mapping)

例: "int: {0:d}; hex: {0;x}; oct: {0:o}; bin: {0:b}".format(100)

'int: 100 ; hex: 64 ; oct: 144; bin: 1100100'

str.format_map( '{name:s}, {age: d}, {weight: 3.2f}' , {'name':'Mary', 'age':20 , 'weight'         : 49})             输出:  'Mary,20,49.00'

二.字节系列

1.bytes常量

b' '  ;   b "  " ;   b"'  '"; 只能为ASCII字符

创建bytes对象: bytes()  空bytes对象

bytes(n)  长度为n的bytes对象,各字节为0

bytes(iterable)    使用iterable中的字节整数   不能超过0~256

2.bytearray   创建bytearray对象:同bytes

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值