Python3 - 常见字符串函数
str1.split():过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串
str1 = ‘I am so Happy,HHH’
str2 = ‘八百标兵奔北坡’
print(str1.split(‘ ‘, 2))
print(str2.split(‘兵’, 2))str1.splitlines():按照行(‘\r’, ‘\r\n’, \n’)分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。
str2 = ‘\r八百标兵\r奔北坡’
print(str2)
print(str2.splitlines())str1.join(): 用于将序列中的元素以指定的字符连接生成一个新的字符串。
str2 = ‘八百标兵奔北坡’
str3 = ‘123’
print(str3.join(str2))max():返回给定参数的最大值,参数可以为序列
list1 = [1, 2, 3]
print(max(list1))min():返回字符串中最小的字母。
list1 = [1, 2, 3]
print(min(list1))str1.replace(old, new[, max]):把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。
print(str1.replace(‘H’,’h’))
print(str1.replace(‘H’,’A’,1))
print(str1.replace(‘H’,’h’,2))
print(str1.replace(‘so’,’SO’))str1.maketrans():返回字符串转换后生成的新字符串。
intab = “abcd”
outtab = “*&^%”
str4 = “I am so cool”
print(str4.translate(str.maketrans(intab, outtab)))str1.translate(table[, delete]):返回翻译后的字符串,若给出了 delete 参数,则将原来的bytes中的属于delete的字符删除,剩下的字符要按照table中给出的映射来进行映射 。
intab = “abcde”
outtab = “12345”
trantab1 = str.maketrans(intab, outtab)
print(trantab1) # {97: 49, 98: 50, 99: 51, 100: 52, 101: 53}
str5 = “this is string example” # this is string 5x1mpl5
print(str5.translate(trantab1))
print(b”this is string example”.translate(None, b’as’))str1.startswith(str, beg=0,end=len(string)):方法用于检查字符串是否是以指定子字符串开头,如果是则返回 True,否则返回 False。如果参数 beg 和 end 指定值,则在指定范围内检查。
print(str5.startswith(‘this’)) # True
print(str5.startswith(‘is’)) # False
print(str5.startswith( ‘is’, 5, 7 )) # beg 起始位置, end结束位置str1.endswith(suffix[, start[, end]]):方法用于判断字符串是否以指定后缀结尾,如果以指定后缀结尾返回True,否则返回False。可选参数”start”与”end”为检索字符串的开始与结束位置
print(str5.endswith(‘le’)) # True
str1.encode():指定的编码格式编码字符串
str6 = ‘八百标兵奔北坡’
print(str6.encode(‘utf-8’))
print(str6.encode(‘GBK’))bytes.decode():以指定的编码格式解码 bytes 对象。默认编码为 ‘utf-8’。
print(bytes.decode(str6.encode(‘utf-8’)))
str1.isalpha():方法检测字符串是否只由字母组成。
str7 = ‘123abc’
str8 = ‘abd’
print(str7.isalpha())
print(str8.isalpha())str1.isalnum():检测字符串是否由字母和数字组成。
print(str7.isalnum())
str1.isupper():检测字符串中所有的字母是否都为大写。
str9 = ‘ABC’
print(str8.isupper())
print(str9.isupper())str1.islower():检测字符串是否由小写字母组成。
print(str8.islower())
str1.istitle():检测字符串中所有的单词拼写首字母是否为大写,且其他字母为小写。
str10 = ‘Abc’
print(str9.istitle())
print(str10.istitle())str1.isdigit():检测字符串是否只由数字组成。
print(str.isdigit(‘123’))
print(str.isdigit(‘123abc’))str1.isnumeric():检测字符串是否只由数字组成。这种方法是只针对unicode对象
print(str.isnumeric(‘123’))
print(str.isnumeric(‘测试’, b’123’))str1.isdecimal():检查字符串是否只包含十进制字符。这种方法只存在于unicode对象
print(str.isdigit(‘12’))
print(str.isdigit(‘15e’))
print(str.isdigit(‘12,101’))str1.isspbace():检测字符串是否只由空白字符组成
print(str.isspace(‘ ‘))
print(str.isspace(‘ 123’))len():返回对象(字符、列表、元组等)长度或项目个数。
print(len(‘123’))
print(len([1, 2, 3]))
print(len((1, 2)))lower():转换字符串中所有大写字符为小写。
print(str.lower(‘ZXCVbnm’))
upper():将字符串中的小写字母转为大写字母。
print(str.upper(‘ZXCVbnm’))
swapcase():用于对字符串的大小写字母进行转换。
print(str.swapcase(‘ZwCVAnm’))
capitalize():将字符串的第一个字母变成大写,其他字母变小写
print(str.capitalize(‘zqwertyuiQWERTYUI’))
title():返回”标题化”的字符串,就是说所有单词都是以大写开始
print(str.title(‘today is a happy day’))
center(width[, fillchar]):返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。
print(str.center(‘today is a happy day’, 30, ‘*’))
ljust(width[, fillchar]):返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。
print(str.ljust(‘today is a happy day’, 30, ‘/‘))
rjust(width[, fillchar]):返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串。如果指定的长度小于字符串的长度则返回原字符串。
print(str.rjust(‘today is a happy day’, 30))
zfill(width):返回指定长度的字符串,原字符串右对齐,前面填充0。
print(str.zfill(‘today is a happy day’,30))
count():统计字符串里某个字符出现的次数。可选参数为在字符串搜索的开始与结束位置
print(str.count(‘today is a happy day’,’a’, 5, 25))
find():方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,如果指定范围内如果包含指定索引值,返回的是索引值在字符串中的起始位置。如果不包含索引值,返回-1。
print(str.find(‘today is a happy day’,’day’))
rfind():返回字符串最后一次出现的位置,如果没有匹配项则返回-1。
print(str.rfind(‘today is a happy day’,’day’))
print(str.rfind(‘today is a happy day’,’nice’))index():方法检测字符串中是否包含子字符串 str ,如果指定 beg(开始) 和 end(结束) 范围,则检查是否包含在指定范围内,该方法与 python find()方法一样,只不过如果str不在 string中会报一个异常。
print(str.index(‘today is a happy day’,’day’))
print(str.index(‘today is a happy day’,’nice’)) # 报错rindex(str, beg=0 end=len(string)):返回子字符串 str 在字符串中最后出现的位置,如果没有匹配的字符串会报异常,你可以指定可选参数[beg:end]设置查找的区间。
print(‘rindex测试’, str.rindex(‘today is a happy day’,’day’))
strip():用于移除字符串头尾指定的字符(默认为空格)
print(str.strip(‘ today is a happy day ‘))
print(str.strip(‘today is a happy day’,’t’))
print(str.strip(‘today is a happy day’,’y’))lstrip():方法用于截掉字符串左边的空格或指定字符。
print(str.lstrip(‘ today is a happy day ‘))
rstrip():删除 string 字符串末尾的指定字符(默认为空格)
print(str.rstrip(‘ today is a happy day’,’day’))
评论系统未开启,无法评论!