Python字符串的内建函数

为了处理字符串,Python提供了很多的内建函数,如查找子串,统计字符串长度,大小写转换等,这里将介绍常用内建函数的用法。

一、find函数

find函数用于查找一个字符串的子串。找到满足条件的子串,则返回子串第一个字符在原始字符串中的索引,否则返回-1.find函数还支持设置查找范围,搜索指定范围内的子串,具体如下,其中第8行和第11行,find函数的第二个参数表示查找的起始位置,第三个参数表示查找的结束位置。

content="where it's working on 5G in ways you may not be expecting"
result=content.find("5G")
print("查找子串5G:",result)

result=content.find("hello")
print("查找子串hello:",result)

result=content.find("5G",23,30)
print("在指定范围内查找子串5G:",result)

result=content.find("5G",22,25)
print("在指定范围内查找子串5G:",result)

执行结果如下,输出在各种查找方式下的搜索结果。

查找子串5G: 22
查找子串hello: -1
在指定范围内查找子串5G: -1
在指定范围内查找子串5G: 22

二、count函数

count函数用于统计一个字符串的子串出现次数。count函数同样支持设置查找范围,统计指定范围内的子串,具体如下。第5行中coun函数的第二个参数表示查找的起始位置,第三个参数表示查找的结束位置。

content="hello world,hello python"
result=content.count("hello")
print("统计子串hello出现次数:",result)

result=content.count("hello",0,15)
print("统计指定范围内hello出现次数:",result)

执行结果如下,输出出现次数

统计子串hello出现次数: 2
统计指定范围内hello出现次数: 1

三、replace函数

replace函数用于把原始字符串中的部分字符替换成新字符。replace函数的第三个参数,用于指定最大替换次数,如下

content="hello world,hello python,hello bigdata"
result=content.replace("hello","你好")
print("替换后的字符串:",result)

result=content.replace("hello","你好",2)
print("指定最多替换次数:",result)

执行结果如下,输出替换结果

替换后的字符串: 你好 world,你好 python,你好 bigdata
指定最多替换次数: 你好 world,你好 python,hello bigdata

四、split函数

split函数用于分割字符串,并将分割后的结果以列表形式返回。split函数的第二个参数,用于指定分割多少个字符串,如下

content="hello world,hello python,hello bigdata"
result=content.split(" ")
print("使用空格进行分割:",result)
result=content.split(" ",1)
print("指定分割第1个空格:",result)

执行结果如下,输出分割结果

使用空格进行分割: ['hello', 'world,hello', 'python,hello', 'bigdata']
指定分割第1个空格: ['hello', 'world,hello python,hello bigdata']

五、title函数与capitalize函数

title函数与capitalize函数都是将字符串以标题化的方式返回,实际上就是将单词首字母大写,如下

content="hello world,hello python"
result=content.title()
print("使用title标题化:",result)

result=content.capitalize()
print("使用capitalize标题化:",result)

执行结果如下,输出标题化后的字符串,可以看到,title函数会将字符串中的所有单词首字母转换为大写,capitalize函数只会将第一个单词首字母转换为大写。

使用title标题化: Hello World,Hello Python
使用capitalize标题化: Hello world,hello python

六、startswith函数与endswith函数

startswith函数用于检测字符串是否以某个子串作为开头,endswith函数则是用于检测是否以某个子串作为结尾。startswith函数与endswith函数的第二个参数表示检测的起始位置,第三个参数表示检测的结束位置,如下

content="hello world,hello python"
result=content.startswith("hello")
print("检测字符串是否以hello作为开头:",result)

result=content.startswith("world")
print("检测字符串是否以world作为开头:",result)

result=content.startswith("world",6,15)
print("检测字符串位置6~15之间的子串是否以world作为开头:",result)

result=content.startswith("python")
print("检测字符串是否以python作为结尾:",result)

执行结果如下,输出检测结果

检测字符串是否以hello作为开头: True
检测字符串是否以world作为开头: False
检测字符串位置6~15之间的子串是否以world作为开头: True
检测字符串是否以python作为结尾: False

七、lower函数与upper函数

lower函数用于将字符串全部转换为小写,upper函数则是将字符串全部转换为大写,如下

content="Hello World,Hello Python"
result=content.lower()
print("将字符转换为小写:",result)

result=content.upper()
print("将字符转换为大写:",result)

执行结果如下,转换输出结果

将字符转换为小写: hello world,hello python
将字符转换为大写: HELLO WORLD,HELLO PYTHON

八、center函数

center函数用于将原始字符串扩展到指定长度并居中。字符串的其余部分使用第二个参数进行填充。若是第二个参数未指定,则使用空格填充,如下

content="hello"
result=content.center(11)
print("默认使用空格填充:",result)

result=content.center(11,"-")
print("使用短横线填充:",result)

执行结果如下,输出居中与填充结果

默认使用空格填充:    hello   
使用短横线填充: ---hello---

九、strip函数

strip函数用于移除字符串首尾的指定字符或子串,若是未指定参数,则默认移除空格,如下

content="hello"
result=content.strip()
print("移除空格:",result)

result=content.strip("lo")
print("移除末尾的lo:",result)

执行结果如下,删除首尾的字符。注意,要删除的" l “有两个,因此剩余的部分是” he "

移除空格: hello
移除末尾的lo: he

十、translate函数

translate函数用于替换字符。与replace函数不同的是,replace函数是按字符串组合进行替换的,而translate函数则是对单个字符进行替换,因此translate函数的执行效率高于replace函数。在使用translate函数替换之前,需要调用str对象的maketrans方法,用于创建替换的映射表。maketrans方法中的第一个参数是被替换的字符,第二个参数是新的字符,两者的长度需要一致。maketrans方法创建的映射表示两个参数的各个字符对应的Unicode码的映射关系,maketrans方法的第三个参数,用于指定要删除的字符,如下

content="hello,world"
table=str.maketrans("lo","ab")
print("Unicode码之间的映射关系:",table)

result=content.translate(table)
print("转换结果:",result)

table=str.maketrans("lo","ab",",")
result=content.translate(table)
print("转换结果:",result)

执行结果如下,输出转换结果

Unicode码之间的映射关系: {108: 97, 111: 98}
转换结果: heaab,wbrad
转换结果: heaabwbrad

十一、join函数

join函数用于将一个序列快速转换为一个字符串。如下,在第2行,使用空格将数组a的元素连接在一起;在第5行,使用逗号将元素连接在一起。注意,使用join函数将序列转换为字符串时,序列中的元素都应该为字符串类型

a=["hello","world","hello","python"]
result=' '.join(a)
print("使用空格连接数组元素:",result)

result=','.join(a)
print("使用逗号连接数组元素:",result)

执行结果如下,输出结果

使用空格连接数组元素: hello world hello python
使用逗号连接数组元素: hello,world,hello,python

十二、字符串验证函数

除以上常用的字符串外,还有isalnum、isascii、isdigit、islower、isnumeric、isupper等is开头的函数。这些函数用于验证字符串是否是数字、是否为ASCII码、是否小写等,返回布尔类型的结果。这些函数在特定情况下使用,因此这里不在赘述。

本篇文章就到这里结束了,希望能给小伙伴们一些帮助,喜欢的小伙伴们可以点赞支持一下噢!
  • 9
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 10
    评论
Python提供了很多字符串内置函数,这里列举几个比较常用的: 1. `len(str)`:返回字符串的长度。 ```python str = "hello, world!" print(len(str)) # 输:13 ``` 2. `str.upper()`和`str.lower()`:将字符串分别转化为大写和小写形式。 ```python str = "Hello, WoRlD!" print(str.upper()) # 输:HELLO, WORLD! print(str.lower()) # 输:hello, world! ``` 3. `str.capitalize()`和`str.title()`:将字符串的首字母或每个单词的首字母转化为大写。 ```python str = "hello, world!" print(str.capitalize()) # 输:Hello, world! print(str.title()) # 输:Hello, World! ``` 4. `str.find(sub, start, end)`和`str.index(sub, start, end)`:返回子字符串在原字符串的位置,若没有则返回-1或抛异常。 ```python str = "hello, world!" print(str.find('o')) # 输:4 print(str.index('o')) # 输:4 print(str.find('z')) # 输:-1 # print(str.index('z')) # 抛异常:ValueError: substring not found ``` 5. `str.count(sub, start, end)`:返回子字符串在原字符串现的次数。 ```python str = "hello, world!" print(str.count('o')) # 输:2 ``` 6. `str.replace(old, new, count)`:将字符串的所有旧子字符串替换为新子字符串,count为替换次数,可省略,表示替换所有。 ```python str = "hello, world!" print(str.replace('l', 'L')) # 输:heLLo, worLd! ``` 除此之外,还有很多其他的字符串内置函数,比如`str.startswith(prefix, start, end)`、`str.endswith(suffix, start, end)`、`str.strip(chars)`、`str.join(iterable)`等等。这些函数都有其特定的功能和用法,可以根据实际情况进行选择和使用。 引用:Python字符串内置函数功能与用法总结。主要介绍了Python字符串内置函数功能与用法,结合实例形式总结分析了Python常见字符串操作函数的功能、分类、使用方法及相关操作注意事项,需要的朋友可以参考下[^1]。 引用:python string内置函数表格。string.replace(str1, str2, num=string.count(str1)) [^2]。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

8X_I

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值