python字符串处理函数

# capitalized() 将字符串第一个字母大写,其余字母全部小写
# 使用方法 str.capitalize()
print('nihao WUHAN'.capitalize())  # Nihao wuhan
# 如果第一个字符不是字母,那么首字母不会有改变,其余全部小写
print('123nihao WUHAN'.capitalize())  # 123nihao wuhan

# center(width, fillchar)  用fillchar将str填充成一个width长度的字符串,str在新的字符串中居中
str1 = 'nihao WUHAN'
print(str1.center(20, '='))  # ====nihao WUHAN=====

# str.ljust(width, fillchar)  # 用fillchar将str填充成一个width长度的字符串,str在新的字符串中左对齐
str1 = 'nihao WUHAN'
print(str1.ljust(20, '='))  # nihao WUHAN======

# str.rjust(width, fillchar)  # 用fillchar将str填充成一个width长度的字符串,str在新的字符串中右对齐
str1 = 'nihao WUHAN'    
print(str1.rjust(20, '='))  # =====nihao WUHAN

# count(sub, start=0, end=len(string))  统计字符串中sub出现的次数,可指定搜索的起始位置和结束位置
str1 = 'nihao WUHAN'
print('str1.count(\'n\') : %d' % str1.count('n'))  # 1
str2 = str1.lower()
print('str2.count(\'n\') : %d' % str2.count('n'))  # 2

# endswith(suffix, start=0, end=len(string))  判断字符穿是否以suffxi结尾,可以指定开始和结尾索引
print(str1.endswith('an'))  # False
print(str1.endswith('AN'))  # True
print(str2.endswith('o ', 0, 5))  # False
print(str2.endswith('o ', 0, 6))  # True

# expandtabs(tabsize=8)  # 用空格代替tab键,tab键默认是八个空格
str1 = 'ttttttll\tt'
str2 = 'tt\tt'
print(str1)  # 默认情况下\t占位8个字符,将字符串8个8个分为一组,当分到\t位置的一组不满8个时,缺少几个就填充几个空格
print(str1.expandtabs())  # 等价于默认情况下
print(str1.expandtabs(4))  # 设置\t占位4字符,将字符串4个4个分为一组,当分到\t位置的一组不满4个时,缺少几个就填充几个空格
print(str1.expandtabs(3))
print(str2)
print(str2.expandtabs())
print(str2.expandtabs(4))
print(str2.expandtabs(3))

# str.find(str, start=0, end=len(string))  # 检测指定范围内的字符串是否包含str字符串,如果包含,则返回str字符串在原字符串中的开始索引,否则返回-1
str1 = 'hello world'
print(str1.find('wor', 3, 15))  # 末尾索引超出范围也可以正确执行
print(str1.find('hel'))

# str.index(str, start=0, end=len(string))  # 与str.find()方法一样,只不过如果str不在字符串中会报一个异常
# print(str1.index('word'))

# str.isalnum()  # 字符串中至少包含一个字符,且只包含字母、数字、中文则返回True,否则返回False
print('kjashdflkjhask23iu4y2837'.isalnum())  # True
print('sjkdhf 87236y'.isalnum())  # False
print('中国asdfasd'.isalnum())  # True

# str.isalpha()  # 字符串中至少包含一个字符,且只包含字母、中文则返回True,否则返回False
print('123sdf'.isalpha())  # False
print('kjashdf中国'.isalpha())  # True
print('kjashdf中国123'.isalpha())  # False

# str.isdigit()  # 字符串中只包含数字则返回True,否则返回False
print('123456'.isdigit())  # True
print('123sdf'.isdigit())  # False

# str.islower()  # 字符串中包含至少一个区分大小写的字符,并且所有这些字符都是小写,则返回True,否则返回False
print('123sdf'.islower())  # True
print('123Sdf'.islower())  # False
print('sjdhf_sjdhf _sdf i'.islower())  # True

# str.isupper()  # 字符串中包含至少一个区分大小写的字符,并且所有这些字符都是大写,则返回True,否则返回False
print('123sdf'.isupper())  # False
print('123Sdf'.isupper())  # False
print('123SDF'.isupper())
 
# str.numeric()  检测字符串是否只由数字组成,数字可以是: Unicode 数字,全角数字(双字节),罗马数字,汉字数字。指数类似 ² 与分数类似 ½ 也属于数字 
print('123456'.isnumeric())  # True
print('123sdf'.isnumeric())  # False
print('123SDF'.isupper())  # True

# str.isspace()  # 字符串中只包含空格、制表符、换行符则返回True,否则返回False
print('  \t\n'.isspace())  # True
print('  \t_\n'.isspace())  # False

# str.title()  将字符串的每个单词的首字母大写,其余字母小写,非字母后的第一个字母也要大写
print('hello world 90/00/10年代的2g/3g/4g网络'.title())

# str.istitle()  字符串中包含至少一个单词,并且所有单词都以大写字母开头,则返回True,否则返回False
print('Hello World 90/00/10年代的2G/3G/4G网络'.istitle())  # True
print('Hello World 90/00/10年代的2g/3g/4g网络'.istitle())  # False

# str.join(iterable)  # 用str将iterable中的元素连接成一个新的字符串
str1 = '(❤ ω ❤)'
iterable = ['i','like', 'a', 'blue', 'berry',]
print(str1.join(iterable))

# str.lstrip([chars])  # 从左到右删除chars中包含的字符,如果碰到了一个chars中不包含的字符,则停止删除
print('www.example.com'.lstrip('wz.'))
print('ccc.example.com'.lstrip('cmowz.'))
print('mm.example.com'.lstrip('cmowz.'))

print('woo.example.com'.lstrip('oamxwe.'))
'''效果等价于'''
print('woo.example.com'.lstrip('wo.exam'))

print('wzc.example.com'.lstrip('cmowz.'))

# str.rstrip([chars])  # 从右到左删除chars中包含的字符,如果碰到了一个chars中不包含的字符,则停止删除
print('www.example.com'.rstrip('moc.'))
print('ccc.example.com'.rstrip('omc.'))
print('mm.example.com'.rstrip('oelcm.'))
print('mm.example.com'.rstrip('ocm.le'))

# str.strip([chars])  # 同时执行lstrip()和rstrip()方法
str1 = 'wwwttaw.example.com.waaaawtat'
print(str1.strip('aaaaaaaattttttttw'))

# max(str)  # 返回字符串中最大的字符
print(max('hello world'))  # w

# min(str)  # 返回字符串中最小的字符
print(min('helloworld'))  # d
print(min('hello world\t\n'))  #  

# str.replace(old, new, max_times)  # 将字符串中的old子串替换成new子串,如果max_times指定,则替换不超过max_times次
str1 = 'hello world ddllikes the bllueberry'
print(str1.replace('ll', ' LL '))  # he LL o world dd LL ikes the b LL uery
print(str1.replace('ll', ' LL ', 2))  # he LL o world ddllikes the blluery

# str.rfind(str1, start=0, end=len(str))  # 从右向左查找str1子串,返回找到的第一个该子串的索引,如果没找到则返回-1
str1 = 'you are my are best baby'
print(str1.rfind('are'))  # 11
print(str1.find('are'))  # 4

# str.rindex(str1, start=0, end=len(str))  # 与str.rfind()方法一样,只不过如果str1不在字符串中会报一个异常
str1 = 'you are my are best baby'
print(str1.rindex('my'))  # 8
# print(str1.rindex('me'))

# str.split(str1, max_num=-1)  # 以sep为分隔符切片字符串,如果maxsplit指定,则仅分割maxsplit个子串
# str.rsplit(str1, max_num=-1)  # 从右向左分割字符串,如果maxsplit指定,则仅分割maxsplit个子串
str1 = 'www.baidu.com.cn'
print(str1.split('.', 2))  # ['www', 'baidu', 'com.cn']
print(str1.rsplit('.', 2))  # ['www.baidu', 'com', 'cn']

# str.startswith(str1, start=0, end=len(str))  # 判断字符串是否以str1开头,可以指定开始和结尾索引
str1 = 'hello world'
print(str1.startswith('he'))  # True
print(str1.startswith('he', 1))  # False

# str.swapcase()  # 交换字母大小写,大写变小写,小写变大写
str1 = 'Wo de FA?'
print(str1.swapcase())  # wO DE fa?

 

 

你可能感兴趣的:(python,python,开发语言,前端)