欢迎访问宙启技术站
智能推送

常用Python字符串函数大全,让你事半功倍

发布时间:2023-06-18 21:29:41

字符串是Python中最常见的数据类型之一,它是一个由字符构成的序列。在日常的编程中,我们常要处理字符串,如去除空格、截取字符串、替换字符串、拼接字符串等等。本文就来介绍一下Python中常用的字符串函数,让你事半功倍。

1. len(str)

len()函数用于返回字符串的长度,也就是字符的数量。它的语法格式为:len(str)。其中str是需要获取长度的字符串。

示例:

s = "hello world"
print(len(s)) # 输出:11

2. str.lower()

lower()函数用于将字符串中的大写字母转换为小写字母,它的语法格式为:str.lower()。其中str是需要操作的字符串。

示例:

s = "HELLO WORLD"
print(s.lower()) # 输出:hello world

3. str.upper()

upper()函数用于将字符串中的小写字母转换为大写字母,它的语法格式为:str.upper()。其中str是需要操作的字符串。

示例:

s = "hello world"
print(s.upper()) # 输出:HELLO WORLD

4. str.capitalize()

capitalize()函数用于将字符串的 个字符转换为大写字母,其余字符均转换为小写字母,其语法格式为:str.capitalize()。其中str是需要操作的字符串。

示例:

s = "hello world"
print(s.capitalize()) # 输出:Hello world

5. str.title()

title()函数用于将字符串每个单词的首字母转换为大写字母,其余字符均转换为小写字母,其语法格式为:str.title()。其中str是需要操作的字符串。

示例:

s = "hello world"
print(s.title()) # 输出:Hello World

6. str.strip()

strip()函数用于去除字符串中开头和结尾的空格或指定字符,其语法格式为:str.strip([chars])。其中str是需要操作的字符串,chars是可选参数,用于指定需要去除的字符。

示例:

s = "   hello world   "
print(s.strip()) # 输出:hello world

s = "----hello world----"
print(s.strip("-")) # 输出:hello world

7. str.lstrip()

lstrip()函数用于去除字符串左侧开头的空格或指定字符,其语法格式为:str.lstrip([chars])。其中str是需要操作的字符串,chars是可选参数,用于指定需要去除的字符。

示例:

s = "   hello world   "
print(s.lstrip()) # 输出:hello world

s = "----hello world----"
print(s.lstrip("-")) # 输出:hello world----

8. str.rstrip()

rstrip()函数用于去除字符串右侧结尾的空格或指定字符,其语法格式为:str.rstrip([chars])。其中str是需要操作的字符串,chars是可选参数,用于指定需要去除的字符。

示例:

s = "   hello world   "
print(s.rstrip()) # 输出:   hello world

s = "----hello world----"
print(s.rstrip("-")) # 输出:----hello world

9. str.startswith(sub)

startswith()函数用于判断字符串是否以指定的字符串开头,其语法格式为:str.startswith(sub)。其中str是需要判断的字符串,sub是指定的字符串。

示例:

s = "hello world"
print(s.startswith("hello")) # 输出:True

s = "hello world"
print(s.startswith("world")) # 输出:False

10. str.endswith(sub)

endswith()函数用于判断字符串是否以指定的字符串结尾,其语法格式为:str.endswith(sub)。其中str是需要判断的字符串,sub是指定的字符串。

示例:

s = "hello world"
print(s.endswith("world")) # 输出:True

s = "hello world"
print(s.endswith("hello")) # 输出:False

11. str.find(sub)

find()函数用于在字符串中查找指定的子串,如果找到则返回子串 次出现的位置,如果没有找到则返回-1,其语法格式为:str.find(sub)。其中str是需要查找的字符串,sub是指定的子串。

示例:

s = "hello world"
print(s.find("world")) # 输出:6

s = "hello world"
print(s.find("Python")) # 输出:-1

12. str.index(sub)

index()函数用于在字符串中查找指定的子串,如果找到则返回子串 次出现的位置,如果没有找到则抛出异常,其语法格式为:str.index(sub)。其中str是需要查找的字符串,sub是指定的子串。

示例:

s = "hello world"
print(s.index("world")) # 输出:6

s = "hello world"
print(s.index("Python")) # 抛出异常:ValueError: substring not found

13. str.rfind(sub)

rfind()函数用于在字符串中查找指定的子串,如果找到则返回子串最后一次出现的位置,如果没有找到则返回-1,其语法格式为:str.rfind(sub)。其中str是需要查找的字符串,sub是指定的子串。

示例:

s = "hello world"
print(s.rfind("o")) # 输出:7

s = "hello world"
print(s.rfind("Python")) # 输出:-1

14. str.rindex(sub)

rindex()函数用于在字符串中查找指定的子串,如果找到则返回子串最后一次出现的位置,如果没有找到则抛出异常,其语法格式为:str.rindex(sub)。其中str是需要查找的字符串,sub是指定的子串。

示例:

s = "hello world"
print(s.rindex("o")) # 输出:7

s = "hello world"
print(s.rindex("Python")) # 抛出异常:ValueError: substring not found

15. str.count(sub)

count()函数用于统计字符串中指定子串出现的次数,其语法格式为:str.count(sub)。其中str是需要统计的字符串,sub是指定的子串。

示例:

s = "hello world"
print(s.count("o")) # 输出:2

s = "hello world"
print(s.count("Python")) # 输出:0

16. str.replace(old, new[, count])

replace()函数用于将字符串中指定的字符替换为新的字符,其语法格式为:str.replace(old, new[, count])。其中str是需要操作的字符串,old是需要被替换的字符,new是替换后的新字符,count是可选参数,表示替换次数。

示例:

s = "hello world"
print(s.replace("o", "a")) # 输出:hella warld

s = "hello world"
print(s.replace("o", "a", 1)) # 输出:hella world

17. str.split([sep[, maxsplit]])

split()函数用于将字符串按指定的分隔符分割成多个子字符串,并将结果存储在一个列表中返回,其语法格式为:str.split([sep[, maxsplit]])。其中str是需要操作的字符串,sep是分隔符,默认为空格,maxsplit是可选参数,表示最大分割次数。

示例:

s = "hello world"
print(s.split()) # 输出:['hello', 'world']

s = "hello,world"
print(s.split(",")) # 输出:['hello', 'world']

s = "hello world"
print(s.split("o", 1)) # 输出:['hell', ' world']

18. str.join(iterable)

join()函数用于将一个可迭代对象中的元素按照指定的分隔符连接成一个字符串,其语法格式为:str.join(iterable)。其中str是指定的分隔符,iterable是可迭代对象。

示例:

`python

lst = ['hello', 'world']

print(" ".join(lst)) # 输出:hello world

lst = ['hello', 'world']

print("-".join(lst)) # 输出:hello-world