Python---字符串第三篇
1.isprintable
判断字符串中所有字符是否都是可打印字符(in repr())或字符串为空。
test = "sdfsdf"
v = test.isprintable()
print(v)
结果为True
2.isspace
检测字符串是否只由空白字符组成。
test = " " v = test.isspace() print(v) 结果为True
3.istitle
检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。
test = " Return the lowest index in S"
v = test.istitle()
print(v)
结果为False
4.title
返回"标题化"的字符串,所有单词都是以大写开始,其余字母均为小写。
test = " Return the lowest index in S"
v = test.title()
print(v)
结果为 Return The Lowest Index In S
5.join(重点)
用于将序列中的元素以指定的字符连接生成一个新的字符串。
test = ("I","Love","Leslie","!")
v= '*'.join(test)
print(v)
结果为 I*Love*Leslie*!
6.lstrip,rstrip,strip
lstrip() 方法用于截掉字符串左边的空格或指定字符。
test = " I Love Leslie!!! "
v = test.lstrip()
print(v)
test1 = "*****I Love Leslie!!!*****"
v1 = test1.lstrip('*')
print(v1)
结果为
I Love Leslie!!!
I Love Leslie!!!*****
rstrip类似。
strip用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列。
注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。
test = "00000003210Leslie01230000000"
print(test.strip('0'))
str2 = " Leslie "
print(str2.strip())
结果为
3210Leslie0123
Leslie
test = "123Leslie321"
print(test.strip('12'))
结果为 3Leslie3
7.maketrans和translate组合使用可以将指定的字符串替换原有字符串。
v = "aeiouaeiouuoieauoiea!!!****"
m = str.maketrans("ai!","@#?")
new_v = v.translate(m)
print(new_v)
结果为 @e#ou@e#ouuo#e@uo#e@???****
8.partition
用来根据指定的分隔符将字符串进行分割。如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。
test = "dfsegfwe"
v = test.partition('s')
print(v)
结果为 ('df', 's', 'egfwe')
****rpartition****>>>细微区别在于前者从目标字符串的末尾也就是右边开始搜索分割符。
test = "dfsegfswe"
v = test.rpartition('s')
print(v)
结果为 ('dfsegf', 's', 'we')
9.spilt
通过指定分隔符对字符串进行分割并返回一个列表,默认分隔符为所有空字符,包括空格、换行(\n)、制表符(\t)等。
-
l Str.split()默认以空格,换行\n,制表符\t分割
-
lStr.split(‘字符串’):以字符串为分割
-
l Str.split(‘字符串’,2):分割以前2次出现的字符串为分割
# 中间是空格 str ='I am is jiyanjiao' print('str =I am is jiyanjiao 分割后的结果:',str.split()) #中间是换行符 str1 = 'I ' \ 'am' \ 'is' \ 'jiyanjiao' print('str1 n/ 换行 分割后的结果:',str.split()) #中间是制表符 str2='I am is jiyanjiao' print('str2 t/ 制表符分割后的结果:',str2.split()) # 以is分割 print('str以is为分割后的结果:',str.split('is')) #以字母i为分割 print('str以字母i为分割后的结果:',str.split('i',2)) ''' 结果为 str =I am is jiyanjiao 分割后的结果: ['I', 'am', 'is', 'jiyanjiao'] str1 n/ 换行 分割后的结果: ['I', 'am', 'is', 'jiyanjiao'] str2 t/ 制表符分割后的结果: ['I', 'am', 'is', 'jiyanjiao'] str以is为分割后的结果: ['I am ', ' jiyanjiao'] str以字母i为分割后的结果: ['I am ', 's j', 'yanjiao'] '''rsplit类似与split()方法,只不过是从后面开始分割。
S = "this is string example....wow!!!" print (S.rsplit( )) print (S.rsplit('i',1)) print (S.rsplit('w')) 结果为 ['this', 'is', 'string', 'example....wow!!!'] ['this is str', 'ng example....wow!!!'] ['this is string example....', 'o', '!!!']
10.splitlines
按照行('\r', '\r\n', \n')分隔,返回一个包含各行作为元素的列表,如果参数为 False,不包含换行符,如果为 True,则保留换行符。
test = "asdf\nasdf\nasdf\n"
v1 = test.splitlines(False)
print(v1)
v2 = test.splitlines(True)
print(v2)
结果为
['asdf', 'asdf', 'asdf']
['asdf\n', 'asdf\n', 'asdf\n']
11.replace
替换字符串,指定参数,表示替换几次。
test = "i love leslie and you too...."
v1 = test.replace('i',"yr")
print(v1)
v2 = test.replace('i',"*",1)
print(v2)
结果为
yr love leslyre and you too....
* love leslie and you too....

浙公网安备 33010602011771号