1. 全部的标准序列操做都使用于字符串,记住字符串不可改变。api
2. 字符串格式化:标记转换说明符(%),若是字符串自己包含%,用%%代替。ide
3. 字符串格式化转换标志:-表示左对齐;+表示在转换值前加正负号;""(空白字符)表示正数以前保留空格;0表示转换值若位数不够用0填充。函数
4. find(str, begin_index, end_index): 返回子串所在位置最左端索引,没有找到返回-1. 注意begin_index和end_index是左闭右开;this
5. join(seq): 在队列中添加元素spa
>>> dirs='','usr','bin','env'索引
>>> '/'.join(dirs)队列
'/usr/bin/env'ip
>>> dirs字符串
('', 'usr', 'bin', 'env')string
6. lower()/upper()/islower()/isupper(): 用于字符串大小写处理;
7. replace(): 返回某子字符串被替换以后的字符串;
8. split(): 将字符串分隔成序列,join()的逆方法,默认以空格/制表/换行为分隔符;
9. strip(x): 去除字符串两侧的制定字符x,默认去除空格
>>> '*** SPAM * for * everyone!!! ***'.strip(' !*')
'SPAM * for * everyone'
10. translate(): 和replace()相似,可是只能处理单个字符,能够同时进行多个替换,第二个参数指定要删除的字符,需与maketrans函数配合:
>>> from string import maketrans
>>> table=maketrans('cs', 'kz')
>>> 'this is an incredible test'.translate(table,' ')
'thizizaninkredibletezt'
11. 其余经常使用的字符串方法:
S.capitalize() #首字母大写
S.istitle() #S是不是首字母大写的
S.isupper() #S中的字母是否全是大写
S.islower() #S中的字母是否全是小写
S.lstrip() #去掉字符串的左边空格
S.rstrip() #去掉字符串的右边空格
S.startwith(prefix[,start[,end]]) #是否以prefix开头
S.endwith(suffix[,start[,end]]) #以suffix结尾
S.index(substr, [start, [end]]) #与find()相同,只是在S中没有substr时,会返回一个运行时错误
S.rindex(substr, [start, [end]])
S.rfind(substr, [start, [end]]) #从右边算起的第一次出现的substr的首字母标号,若是S中没有substr则返回-1
S.count(substr, [start, [end]]) #计算substr在S中出现的次数
12. string模块的几个经常使用函数:
string.atoi(s[,base]) #base默认为10,若是为0,那么s就能够是012或0x23这种形式的字符串,若是是16那么s就只能是0x23或0X12这种形式的字符串
string.atol(s[,base]) #转成long
string.atof(s[,base]) #转成float