python的编译器对于字符串类型提供了大量的内置处理函数。
可以用于字符串的检测、替换、排版、转换、判断等。
值得注意的是,该种方式对字符串本身是不做更改的,而是进行修改之后返回一个新的字符串,并不影响原先的字符串。
以下的字符串处理方法都有其最简单的使用形式,以及较为复杂进阶的使用方式,笔者暂且只介绍简单的形式用法。
该种内置字符串处理方法大致可分为七种:
- 字符串查找类
- 字符串分隔类
- 字符串连接类
- 字符串大小写转换类
- 字符串修改类
- 字符串判断类
- 字符串排版类
一、字符串查找类
1.find()
从左往右查找子字符串 sub 在主字符串中第一次出现的位置(下标),若存在则返回该位置,否则返回 -1。
str = "Hello World and Python is Beautiful"
print(str.find("o"))
# 输出结果为 4
print(str.find("Python"))
# 输出结果为 16
print(str.find("C++"))
# 输出结果为 -1
2.rfind()
从从字符串的末尾开始查找子字符串 sub 在主字符串中最后一次出现的位置(下标),若存在则返回该位置,否则返回 -1。
str = "Hello World and Python is Beautiful"
print(str.rfind("o"))
# 输出结果为 20
print(str.rfind("Python"))
# 输出结果为 16
print(str.rfind("C++"))
# 输出结果为 -1
3.index()
与 find()
方法类似,从左往右查找子字符串 sub 在主字符串中第一次出现的位置(下标),不同之处在于如果子字符串 sub 不存在,会抛出 ValueError 异常。需要注意的是,这种异常情况需要进行处理,以避免程序崩溃。
str = "Hello World and Python is Beautiful"
print(str.index("o"))
# 输出结果为 4
print(str.index("Python"))
# 输出结果为 16
4.rindex()
rindex() 用于查找一个字符串在另一个字符串中最后一次出现的位置,如果子字符串 sub 不存在,会抛出 ValueError 异常。需要注意的是,这种异常情况需要进行处理,以避免程序崩溃。
s = 'hello world, welcome to Python world!'
index_1 = s.rindex('world')
print(index_1) # 输出 31
index_2 = s.rindex('Python')
print(index_2) # 输出 24
# 如果查找的子字符串不存在于原字符串中,会抛出 ValueError 异常
index_3 = s.rindex('Java')
# 抛出 ValueError: substring not found
5.count()
count() 方法用于统计指定子字符串在原字符串中出现的次数。
s = 'hello world, welcome to Python world!'
count1 = s.count('world')
print(count1) # 输出 2
count2 = s.count('Python')
print(count2) # 输出 1
count3 = s.count('Java')
print(count3) # 输出 0
count4 = s.count('e')
print(count4) # 输出 3
count5 = s.count('E')
print(count5) # 输出 0
需要注意的是,count() 方法区分大小写,即大写字母和小写字母被视为不同的字符。如果想要不区分大小写,可以先将字符串全部转为小写或大写再进行统计。
s = 'hello world, welcome to Python world!'
count4 = s.count('e')
print(count4) # 输出 3
count5 = s.count('E')
print(count5) # 输出 0
二、字符串分隔类
1.split()
split() 方法用于将字符串按照指定的分隔符分割成由多个子字符串组成的列表。
str.split(sep=None, maxsplit=-1)
其中,str 表示要分割的字符串,sep 表示进行分割的依据,如果没有内容,默认使用空格作为分隔符。maxsplit 表示最大分割次数,如果不指定或者指定为 -1,表示会分割所有可以分割的部分。
s = 'hello world, welcome to Python world!'
list1 = s.split()
print(list1) # 输出 ['hello', 'world,', 'welcome', 'to', 'Python', 'world!']
list2 = s.split(',')
print(list2) # 输出 ['hello world', ' welcome to Python world!']
值得注意的是,如果分隔符不存在于原字符串中,split() 方法会返回整个字符串的列表形式。同时,如果分隔符位置出现在字符串开头或结尾,split() 方法会将其分割成一个空字符串并加入到列表中,例如:
s = ',hello,world,'
list3 = s.split(',')
print(list3) # 输出 ['', 'hello', 'world', '']
2.rsplit()
rsplit() 方法用于将字符串从字符串的末尾开始,按照指定分隔符分割成多个子字符串。
str.rsplit(sep=None, maxsplit=-1)
其中,str 表示要分割的字符串,sep 表示进行分割的依据,如果没有内容,默认使用空格作为分隔符。maxsplit 表示最大分割次数,如果不指定或者指定为 -1,表示会分割所有可以分割的部分。
s = 'hello,world,Python'
list1 = s.rsplit(',')
print(list1) # 输出 ['hello', 'world', 'Python']
s2 = 'hello,world,Python,,'
list2 = s2.rsplit(',', maxsplit=1)
print(list2) # 输出 ['hello,world,Python,', '']
s3 = 'hello\nworld\nPython'
list3 = s3.rsplit('\n')
print(list3) # 输出 ['hello', 'world', 'Python']
s4 = 'hello world Python'
list4 = s4.rsplit(None, maxsplit=1)
print(list4) # 输出 ['hello world', 'Python']
值得注意的是,如果分隔符不存在于原字符串中,split() 方法会返回整个字符串的列表形式。同时,如果分隔符位置出现在字符串开头或结尾,split() 方法会将其分割成一个空字符串并加入到列表中。
3.partition()
partition()
方法用于将一个字符串划分为两个元素的元组。
str.partition(separator)
其中str是所查找的分隔符(或者不匹配任何部分)之前的字符串,separator是分隔符及其后面的字符串。
partition()
方法只返回一个元组,并且只执行一次查找。
例如:
s = 'hello, world, Python'
result1 = s.partition(',')
print(result1) # 输出 ('hello', ',', ' world, Python')
result2 = s.partition(';')
print(result2) # 输出 ('hello, world, Python', '', '')
result3 = s.partition('l')
print(result3) # 输出 ('he', 'l', 'lo, world, Python')
如果找不到分隔符,那么返回的元组中第一个元素是完整字符串,后面两个元素为空字符串。
4.rpartition()
rpartition()
方法用于从右侧开始查找分隔符并进行划分。
str.rpartition(separator)
其中,str 表示要进行操作的字符串,separator 是划分字符串的分隔符。
s = 'hello, world, Python'
result1 = s.rpartition(',')
print(result1) # 输出 ('hello, world', ',', ' Python')
result2 = s.rpartition(';')
print(result2) # 输出 ('', '', 'hello, world, Python')
result3 = s.rpartition('l')
print(result3) # 输出 ('hello, wor', 'l', 'd, Python')
如果没有找到分隔符,则返回的元组中第三个元素是完整字符串,前面两个元素为空字符串。
三、字符串连接类
join()
四、字符串大小写转换类
1.lower()
2.upper()
3.
五、字符串修改类
六、字符串判断类
七、字符串排版类
(未完待续)