字符串的查询操作
new_str = 'hello,hello,python'
print('使用index()方法查询返回结果',new_str.index('lo')) # 使用index()方法查询返回结果 3
print('使用find()方法查询返回结果',new_str.find('lo')) # 使用find()方法查询返回结果 3
print('使用rindex()方法查询返回结果',new_str.rindex('lo')) # 使用rindex()方法查询返回结果 9
print('使用rfind()方法查询返回结果',new_str.rfind('lo')) # 使用rfind()方法查询返回结果 9
# 查找不存在的值时
# print('使用index()方法查询返回结果',new_str.index('k')) # ValueError: substring not found 未找到子字符串
print('使用find()方法查询返回结果',new_str.find('k')) # 使用find()方法查询返回结果 -1
# print('使用rindex()方法查询返回结果',new_str.rindex('k')) # ValueError: substring not found 未找到子字符串
print('使用rfind()方法查询返回结果',new_str.rfind('k')) # 使用rfind()方法查询返回结果 -1
字符串的大小写转换
# 将字符串全部转换为大写
new_str = 'hello,python,World'
s1 = new_str.upper()
print('字符串全部转换为大写',new_str.upper(),id(new_str.upper()),'\t',new_str,id(new_str))
# print('判断字符串是否全部为大写,True:满足条件;False:则不满足条件',new_str.isupper())
# 将字符串全部转换为小写
print('字符串全部转换为小写',s1.lower(),id(s1.lower()),'\t',s1,id(s1))
print('判断字符串是否全部为小写,True:满足条件;False:则不满足条件',new_str.islower())
# 将字符串大小写互相转换
print('字符串大小写转换后结果为:',new_str.swapcase())
print('字符串首字母转换为大写:',new_str.title())
字符串内容如何对齐?
new_str = 'hello,Python'
# 设置字符串居中对齐 center([参数1:指定宽度,宽度小于等于原字符串宽度时则返回原字符串],[参数2:指定填充符,可选,默认为空格])
print('使用center()方法设置字符串居中对齐',new_str.center(20,'*'))
# 设置左对齐 ljust([参数1:指定宽度,宽度小于原字符串宽度时则返回原字符串],[参数2:指定填充符,可选,默认为空格])
print('使用ljust()方法设置字符串左对齐',new_str.ljust(20,'*'))
# 设置右对齐 rjust([参数1:指定宽度,宽度小于等于原字符串宽度时则返回原字符串],[参数2:指定填充符,可选,默认为空格])
print('使用rjust()方法设置字符串右对齐',new_str.rjust(20,'*'))
# 设置右对齐 zfill([参数1:指定宽度,宽度小于等于原字符串宽度时则返回原字符串;左边用0填充])
print('使用zfill()方法设置字符串右对齐',new_str.zfill(20))
# 不常见的一些情况
print('字符串为一串负的数字','-8268'.zfill(8)) # 字符串为一串负的数字 -0008268
字符串常用的分割方法
new_str = 'hello world Python'
# 使用 split()方法分割字符串,从左边依次分割(默认以字符串之间的空格为分割点),返回值是一个列表
print('使用split()方法分割字符串',new_str.split())
new_str = 'hello&world&Python'
# 使用 split(sep=)方法分割字符串,从左边依次分割(sep='指定分割字符串的参数'),返回值是一个列表
print('使用split(sep=)方法分割字符串',new_str.split(sep='&'))
''' 使用 split(sep=,maxsplit=)方法分割字符串,从左边依次分割
(sep='指定分割字符串的参数',maxsplit='指定最大分割次数,指定次数未分割完的字符串会单独作为一部分'),返回值是一个列表 '''
print('使用 split(sep=,maxsplit=)方法分割字符串',new_str.split(sep='&',maxsplit=1)) # ['hello', 'world&Python']
''' 使用 rsplit(sep=,maxsplit=)方法分割字符串,从右边依次分割
(sep='指定分割字符串的参数',maxsplit='指定最大分割次数,指定次数未分割完的字符串会单独作为一部分'),返回值是一个列表 '''
print('使用 rsplit(sep=,maxsplit=)方法分割字符串',new_str.rsplit(sep='&',maxsplit=1)) # ['hello&world', 'Python']
字符串的常用判断方法
new_str = 'hellp,Python'
# 判断指定的字符串是否为合法的标识符 (True:合法;False:非法)[合法标识符:字母、数字、下划线]
print('1.',new_str.isidentifier()) # False
print('2.','hello'.isidentifier()) # True
print('3.','张三_'.isidentifier()) # True
print('4.','张三_123'.isidentifier()) # True
print('--------------------------------------------------------------------')
# 判断指定的字符串是否全部由空白字符组成 (True:是;False:否)[空白字符:回车、换行、水平制表符]
print('5.','\t'.isspace()) # True
print('--------------------------------------------------------------------')
# 判断指定的字符串是否全部由字母组成 (True:是;False:否)
print('6.','abc'.isalpha()) # True
print('7.','张三'.isalpha()) # True
print('8.','张三123'.isalpha()) # False
print('--------------------------------------------------------------------')
# 判断指定的字符串是否全部由十进制的数字组成 (True:是;False:否)
print('9.','123'.isdecimal()) # True
print('10.','123四'.isdecimal()) # False
print('11.','ⅡⅡⅡ'.isdecimal()) # False
print('--------------------------------------------------------------------')
# 判断指定的字符串是否全部由数字组成 (True:是;False:否)
print('12.','123'.isnumeric()) # True
print('13.','123四'.isnumeric()) # True
print('14.','ⅡⅡⅡ'.isnumeric()) # False
print('--------------------------------------------------------------------')
# 判断指定的字符串是否全部由字母和数字组成 (True:是;False:否)
print('15.','123a'.isalnum()) # True
print('16.','123张三'.isalnum()) # True
print('17.','abc!'.isalnum()) # False
字符串的替换与合并
new_str = 'hello,Python,Python,Python'
# 使用 .replace('参数1','参数2')方法替换字符串 [参数1:指定被替换的子串,参数2:指定替换子串的字符串] 返回替换后得到的字符串
print('使用 .replace()方法替换字符串',new_str.replace('Python','Java'))
# 使用 .replace('参数1','参数2','参数3')方法替换字符串 [参数1:指定被替换的子串,参数2:指定替换子串的字符串,参数3:指定最大替换次数]
print('使用 .replace()方法替换字符串',new_str.replace('Python','Java',2))
# 使用 .join()方法将元祖或列表中的字符串合并成一个字符串
new_lst = ['hello','Python','Java'] # 列表
print('拼接列表字符串','|'.join(new_lst),'\t',''.join(new_lst)) # hello|Python|Java helloPythonJava
new_tuple = ('hello','Python','Java') # 元组
print('拼接元组字符串','|'.join(new_lst),'\t',''.join(new_lst)) # hello|Python|Java helloPythonJava
print('直接拼接具体的字符串','*'.join('Python')) # P*y*t*h*o*n
本文暂时没有评论,来添加一个吧(●'◡'●)