python的内置的字符串处理方法(基础)

python的编译器对于字符串类型提供了大量的内置处理函数。

可以用于字符串的检测、替换、排版、转换、判断等。

值得注意的是,该种方式对字符串本身是不做更改的,而是进行修改之后返回一个新的字符串,并不影响原先的字符串。

以下的字符串处理方法都有其最简单的使用形式,以及较为复杂进阶的使用方式,笔者暂且只介绍简单的形式用法。

该种内置字符串处理方法大致可分为七种:

  1. 字符串查找类
  2. 字符串分隔类
  3. 字符串连接类
  4. 字符串大小写转换类
  5. 字符串修改类
  6. 字符串判断类
  7. 字符串排版类

一、字符串查找类

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.

五、字符串修改类

六、字符串判断类

七、字符串排版类

(未完待续)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值