1.python之split()
描述:通过指定分隔符对字符串进行切分
语法:
str.split(str="", num=string.count(str)).
参数:str :分隔符 num:分割次数
举例:
str = "Line1-abcdef \nLine2-abc \nLine4-abcd";
print str.split( ); # 以空格为分隔符,包含 \n
print str.split(' ', 1 ); # 以空格为分隔符,分隔成两个
-------结果如下-------
['Line1-abcdef', 'Line2-abc', 'Line4-abcd']
['Line1-abcdef', '\nLine2-abc \nLine4-abcd']
txt = "Google#Runoob#Taobao#Facebook"
# 第二个参数为 1,返回两个参数列表
x = txt.split("#", 1)
--------结果如下------
['Google', 'Runoob#Taobao#Facebook']
2.python之replace()
描述:把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次
语法:str.replace(old, new[, max])
参数:
old -- 将被替换的子字符串。
new -- 新字符串,用于替换old子字符串。
max -- 可选字符串, 替换不超过 max 次
举例:
str = "this is string example....wow!!! this is really string";
print str.replace("is", "was");
print str.replace("is", "was", 3);
-------结果如下----
thwas was string example....wow!!! thwas was really string
thwas was string example....wow!!! thwas is really string
3.python之set()
描述:创建一个无重复的元素集
举例:
1.x = set('runoob')
y = set('google')
>>> x, y
(set(['b', 'r', 'u', 'o', 'n']), set(['e', 'o', 'g', 'l'])) # 重复的被删除
2.emails=['testemail@leetcode.com', 'testemail@leetcode.com', 'testemail@lee.tcode.com']
>>>set(emails)
>{'testemail@leetcode.com', 'testemail@lee.tcode.com'}
3.python之zip()
描述:于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组、字典
举例:
a=[1,2,3]
b=[4,5,6]
list(zip(a,b))
>>>[(1, 4), (2, 5), (3, 6)]
dict(zip(a,b))
>>>{1: 4, 2: 5, 3: 6}
4.python 之join()
描述:以指定的字符,将序列中的元素,连接生成一个新的字符串
举例:
s1 = "-"
s2 = ""
seq = ("r", "u", "n", "o", "o", "b") # 字符串序列
print (s1.join( seq ))
print (s2.join( seq ))
>>>r-u-n-o-o-b
>>>runoob