第三章 字符串
3.1 字符串操作类似序列,但字符串不可变
3.2 精简格式化指令
%+格式化字符串 调用:%+希望格式化的值
%s s标记需要插入转化值的位置
%f 格式化浮点数
>>>format=”….%.3f”
>>>from math import pi
>>>print format %pi
3.3 完整格式化指令
%d %i 有符号十进制
%r repr()
%s str()
字段宽度和精度 %宽度.精度f/s
%10f %10.3f %.3f
‘xxx %.*s’ %(5,’…’) #*号代替元组中的5, 5位精度
标表:0/+/-:0表示0填充,-号左对齐,+号标示±号,空格对齐正负号
3.4 字符串方法
find 查找子字符串,没有则返回-1,返回子字符串所在位置最左端索引
join split的逆方法,split割成序列
lower 返回小写字母版本
title 标题化
capwords首词大写开头
replace >>>”str”.replace(‘s’,’z’) #’ztr’
strip 去除字符串两侧的空格