python字符串的相关操作真的有好多:
find, rfind, index, count, replace*, split*, startswith, endswith, strip*, partition*, rpartition,
splitlines, join*, capitalize, title, lower, upper, isalnum, ispace, isdigit, isalpha;
重点说一下加*标的操作:
-
replace,代替,替换;根据指定数据对字符串中的内容进行替换
语法格式: str.replace(“原数据”, “新数据”) or(“原数据”, “新数据”,替换次数)
my_str = “hello”
result = my_str.replace(“l”, “w”) #不指定替换次数,则全部替换
print(result) #返回的结果是一个新的字符串result = my_str.replace(“l”, “w”, 1) #指定替换次数,则替换指定次数
print(result) -
split,分开,使分开(成为几个部分);根据指定的数据对字符串中的数据进行切片,切片后每个子字符串放到列表里
语法格式:str.split(‘指定的用于切片的数据’) or (‘指定的用于切片的数据’, 切片次数) or () 不指定任何参数
my_str = “苹果:香蕉:鸭梨”
result = my_str.split(’:’)
print(result, type(result)) #[‘苹果’, ‘香蕉’, ‘鸭梨’] <class ‘list’> 分离后的结果是列表类型;result = my_str.split(":香蕉:")
print(result, type(result)) #[‘苹果’, ‘鸭梨’] <class ‘list’>; 香蕉和左右两边的分号都被切走了result = my_str.split(":", 1)
print(result, type(result)) #[‘苹果’, ‘香蕉:鸭梨’] <class ‘list’> 只切1次my_str = “a n\tw\ny”
result = my_str.split() #空括号不指定参数,表示按照空白字符(空格,\t,\n)进行切片
print(result) #[‘a’, ‘n’, ‘w’, ‘y’] -
partition,分割; 根据指定数据把字符串分割成三部分,分割后的每个子字符串放在一个元组里
语法格式:str.partition(‘指定的用于分割的数据’)
my_str = “qwe.txt”
result = my_str.partition(".")
print(result,type(result)) #(‘qwe’, ‘.’, ‘txt’) <class ‘tuple’> 只能分割成3部分,且返回类型是元组result = my_str.partition(“e”)
print(result,type(result)) # (‘qw’, ‘e’, ‘.txt’) <class ‘tuple’>my_str = “qwe.txt.png”
result = my_str.rpartition(".") #从右边开始分割
print(result,type(result)) #(‘qwe.txt’, ‘.’, ‘png’) <class ‘tuple’> -
split和partition的对比:
str1 = ‘苹果:鸭梨:葡萄’
result = str1.split(’:’)
print(result) #[‘苹果’, ‘鸭梨’, ‘葡萄’]
result = str1.partition(’:’)
print(result) # (‘苹果’, ‘:’, ‘鸭梨:葡萄’)str2= “qwe.txt”
result = str2.split(".")
print(result) # [‘qwe’, ‘txt’]
result = str2.partition(".")
print(result) # (‘qwe’, ‘.’, ‘txt’)str3 = “qwe.txt.png”
result = str3.split(".")
print(result) # [‘qwe’, ‘txt’, ‘png’]
result = str3.partition(".")
print(result) # (‘qwe’, ‘.’, ‘txt.png’)
总结:split是通过"指定数据"对字符串进行切割,然后把子字符串放到列表,且"指定数据"不会存在于列表中;
partition是通过"指定数据"对字符串进行分割成3部分,3个子字符串存放到元组,且"指定数据"作为其中1个子字符串存放于元组; -
strip, 除去,去除; 用于去除字符串头尾指定的字符,返回的是一个新的字符串;
语法格式:str.strip(‘指定的用于去除的数据’) or ()不指定任何数据
my_str = “!qqq!”
new_str = my_str.strip("!") #按照指定数据去除数据两边的相应字符串
print(new_str)my_str = " anc "
new_str = my_str.strip() #空括号表示去除数据两边的空白字符(空格,\t,\n)
print(new_str) #返回一个新的字符串new_str = my_str.lstrip() #空括号表示去除数据左边的空白字符(空格,\t,\n)
print(new_str) #返回一个新的字符串new_str = my_str.rstrip() #空括号表示去除数据右边的空白字符(空格,\t,\n)
print(new_str) #返回一个新的字符串 -
join,连接,结合;把容器类型中的每一个数据根据指定的字符串进行拼接,返回一个新的字符串
语法格式: ‘需要拼接进去的数据’.join(容器类型) 容器类型包含:字符串,列表,range,元组,集合my_str = “anc”
new_str = “#”.join(my_str)
print(new_str, type(new_str)) # a#n#c <class ‘str’>返回一个新的字符串my_list = [“A”, “1”, “g”]
result = “&”.join(my_list) #对列表里的数据进行拼接
print(result, type(result)) # A&1&g <class ‘str’>返回一个新的字符串
result = “”.join(my_list) # A1g
result = “,”.join(my_list) # A,1,gmy_list = [“A”, 1, “g”]
result = “&”.join(my_list) #对列表里的数据进行拼接时,列表里的数据都必须是字符串,其他的数据类型不行
print(result, type(result)) #TypeError: sequence item 1: expected str instance, int found
总结:容器类型里的数据必须是字符串,才能使用join