string1 ="Hello,World."
string2 ="Python is the best computer language"
string3 ="Python is the language that the most easy of the\tworld."
string4 ="3 is bigger than 1"
string5 ="Python is the {what} that the most {how} of the world."
print(string5.format_map({"what":"test1","how":"test2"}))#与format相同,都是用于格式化替换的,不同之处在于format_map使用字典来同时替换多个字符#Python is the test1 that the most test2 of the world.
print(string3.partition("t"))# 以给定的字符,将字符串分成三段,返回一个元组,从左往右找,找到第一个指定的字符,以此字符,左边的为第一段,自己为第二段,右边的为第三段。#('Py', 't', 'hon is the language that the most easy of the\tworld.')print(string3.partition("that"))# 以给定的字符,将字符串分成三段,返回一个元组,从左往右找,找到第一个指定的字符,以此字符,左边的为第一段,自己为第二段,右边的为第三段。#('Python is the language ', 'that', ' the most easy of the\tworld.')
replace( )–将所有的匹配到的字符串全部替换为新的字符串
print(string3.replace("the","that"))# 如果只给定老的字符串与一个新的字符串,那么将所有的匹配到的字符串全部替换为新的字符串print(string3.replace("the","that",-1))# 默认值为-1,-1就是所有的匹配到的字符串都更换print(string3.replace("the","that",1))# 给定替换的匹配到的个数,比如这里写的1,则只替换第一个print(string3.replace("the","that",2))# 设定为2就替换匹配到的2个"""
Python is that language that that most easy of that world.
Python is that language that that most easy of that world.
Python is that language that the most easy of the world.
Python is that language that that most easy of the world.
"""
print(string3.rsplit("the"))# 以指定字符将字符串分割成多段,默认情况下,只要是匹配到的字符就分,从右往左找,分割符被逗号取代,返回值为列表print(string3.rsplit("the",2))# 以指定字符将字符串分割成多段,默认情况下,只要是匹配到的字符就分,分割符被逗号取代,返回值为列表,默认值为-1,表示不限制,2表示找两个分割符print(string3.rsplit("the",1))#"""
['Python is ', ' language that ', ' most easy of ', '\tworld.']
['Python is the language that ', ' most easy of ', '\tworld.']
['Python is the language that the most easy of ', '\tworld.']
"""
split( )–比较上一个就是从左开始查找关键字,并以关键字进行拆分,从左往右查找
print("*".center(100,"*"))print(string3.split("the"))# 比较上一个就是从左开始查找关键字,并以关键字进行拆分,从左往右查找print(string3.split("the",2))# 2为maxsplit,最多找2个分割符,从左往右查找print(string3.split("the",-1))# -1 为默认值,不高上限print(string3.split("the",1))# 最多查找1个关键字"""
['Python is ', ' language that ', ' most easy of ', '\tworld.']
['Python is ', ' language that ', ' most easy of the\tworld.']
['Python is ', ' language that ', ' most easy of ', '\tworld.']
['Python is ', ' language that the most easy of the\tworld.']
"""
# 将一行字符串分割成多行,并且返回到一个列表中,分割符为\r,\n 可以选择是否保留分割符
string30 ="Python is the\n language\r.that the\r most easy\n of the world."print("*".center(100,"*"))print(string30.splitlines())print(string30.splitlines(keepends=True))"""
****************************************************************************************************
['Python is the', ' language', '.that the', ' most easy', ' of the world.']
['Python is the\n', ' language\r', '.that the\r', ' most easy\n', ' of the world.']
"""