一、字符串函数
1、replace()
函数原型S.replace(old, new[, count]) -> string
比如 str="hello world hello phf"
str.replace("hello","hi") 则会将str里与world匹配的字符串用新的字符串 “hi”代替 输出"hi world hi phf"如果匹配不了原样输出
关于第三个参数 count 用来控制replace 前几个匹配的字符串 比如 str.replace("hello",'hi',1) 则只会用hi代替第一个匹配到的字符串,即输出 "hi world hello phf"
2、capitalize()
函数原型
S.capitalize() -> string
Return a copy of the string S with only its first character
capitalized.
会将字符串S的第一个字符大写然后拷贝一份输出 比如str=“abc”
str.capitalize() 会输出 “Abc”
3、str.split()
函数原型
S.split([sep [,maxsplit]]) -> list of strings
将字符串 以sep为分隔符 分片 如果给出maxsplit 则分片几次 比如
ip="101.7.180.170"
ip.split('.') 输出["101","7","180","170"]
ip.split('.',2)输出['101', '7', '180.170']
二 、 序列的函数
zip(...)
zip(seq1 [, seq2 [...]]) -> [(seq1[0], seq2[0] ...), (...)]
Return a list of tuples, where each tuple contains the i-th element
from each of the argument sequences. The returned list is truncated
in length to the length of the shortest argument sequence.
返回元祖的列表
比如
l1=[1,3,5,7,9]
>>> l2=[2,4,6,8,10]
>>> l3=zip(l1,l2)
>>> l3
[(1, 2), (3, 4), (5, 6), (7, 8), (9, 10)]
如果两个序列长度不匹配 则会截断 即如果 l2=[2,4,6,8] 则会输出[(1, 2), (3, 4), (5, 6), (7, 8)]