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

Python字符串函数大全:轻松掌握字符串操作技巧

发布时间:2023-06-18 07:02:59

Python是一种很强大的编程语言,也是许多数据科学家和程序员的首选。

在Python中,字符串是一种表示文本的数据类型。而且Python有丰富的字符串函数,可以实现各种字符串操作。今天我们就来一探究竟,掌握Python字符串函数的应用技巧。

1. capitalize()函数:将字符串的第一个字母变成大写字母

示例:

str = "hello world"
print (str.capitalize())

输出结果:

Hello world

2. casefold()函数:将字符串所有字符变成小写,并且删除所有的大小写区别

示例:

str = "HeLLo WoRlD"
print(str.casefold())

输出结果:

hello world

3. center()函数:将字符串居中,并在两侧填充指定字符,默认填充空格。

示例:

str = "hello world"
print(str.center(20, '#'))

输出结果:

#####hello world#####

4. count()函数:返回字符串中指定的字符出现的次数

示例:

str = "hello world"
print(str.count('l'))

输出结果:

3

5. endswith()函数:判断字符串是否以指定的字符结尾

示例:

str = "hello world"
print(str.endswith('d'))

输出结果:

True

6. index()函数:返回字符串中指定字符的位置,如果没有出现则抛出异常。

示例:

str = "hello world"
print(str.index('o'))

输出结果:

4

7. isalnum()函数:判断字符串是否只包含字母和数字

示例:

str = "hello world123"
print(str.isalnum())

输出结果:

True

8. isalpha()函数:判断字符串是否只包含字母

示例:

str = "hello world"
print(str.isalpha())

输出结果:

False

9. isdecimal()函数:判断字符串是否只包含十进制数字

示例:

str = "12345"
print(str.isdecimal())

输出结果:

True

10. isdigit()函数:判断字符串是否只包含数字,不包含小数点和正负号

示例:

str = "12345"
print(str.isdigit())

输出结果:

True

11. isidentifier()函数:判断字符串是否是标识符

示例:

str = "hello_world"
print(str.isidentifier())

输出结果:

True

12. islower()函数:判断字符串所有字母是否都是小写字母

示例:

str = "hello world"
print(str.islower())

输出结果:

True

13. isnumeric()函数:判断字符串是否只包含数字

示例:

str = "12345"
print(str.isnumeric())

输出结果:

True

14. isspace()函数:判断字符串是否只包含空格

示例:

str = " "
print(str.isspace())

输出结果:

True

15. istitle()函数:判断字符串的首字母是否为大写

示例:

str = "Hello World"
print(str.istitle())

输出结果:

True

16. isupper()函数:判断字符串所有字母是否都是大写字母

示例:

str = "HELLO WORLD"
print(str.isupper())

输出结果:

True

17. join()函数:将序列中的元素以指定的字符连接生成一个新的字符串

示例:

str = "-".join(["hello", "world"])
print(str)

输出结果:

hello-world

18. ljust()函数:将字符串左对齐,并在右侧填充指定字符,默认填充空格。

示例:

str = "hello world"
print(str.ljust(20, '*'))

输出结果:

hello world**********

19. lower()函数: 将字符串中的所有字母都转为小写字母

示例:

str = "HELLO WORLD"
print(str.lower())

输出结果:

hello world

20. lstrip()函数:截掉字符串左边的空格或指定字符串

示例:

str = "  hello world  "
print(str.lstrip())

输出结果:

hello world  

21. partition()函数:根据指定的分隔符将字符串进行分割,返回一个包含三个元素的元组:分隔符之前的子串、分隔符本身、分隔符之后的子串。

示例:

str = "www.baidu.com"
print(str.partition('.'))

输出结果:

('www', '.', 'baidu.com')

22. replace()函数:将字符串中指定的子串替换成另一个子串

示例:

str = "hello world"
print(str.replace('o', 'x'))

输出结果:

hellx wxrld

23. rfind()函数:查找指定的字符串在字符串中最后一次出现的位置,如果没有找到则返回-1

示例:

str = "hello world"
print(str.rfind('o'))

输出结果:

7

24. rjust()函数:将字符串右对齐,并在左侧填充指定字符,默认填充空格。

示例:

str = "hello world"
print(str.rjust(20, '*'))

输出结果:

**********hello world

25. rpartition()函数:根据指定的分隔符将字符串进行分割,返回一个包含三个元素的元组:分隔符之前的子串、分隔符本身、分隔符之后的子串。从右边开始查找

示例:

str = "www.baidu.com"
print(str.rpartition('.'))

输出结果:

('www.baidu', '.', 'com')

26. rstrip()函数:截掉字符串右边的空格或指定字符串

示例:

str = "  hello world  "
print(str.rstrip())

输出结果:

  hello world

27. split()函数:将字符串以指定的字符进行分割,返回一个列表

示例:

str = "hello,world"
print(str.split(','))

输出结果:

['hello', 'world']

28. splitlines()函数:按照换行符分割字符串,返回一个列表

示例:

str = "hello
world"
print(str.splitlines())

输出结果:

['hello', 'world']

29. startswith()函数:判断字符串是否以指定的字符开头

示例:

str = "hello world"
print(str.startswith('h'))

输出结果:

True

30. strip()函数:截掉字符串左右两边的空格或指定字符

示例:

str = "  hello world  "
print(str.strip())

输出结果:

hello world

31. swapcase()函数:将字符串中小写字母转为大写字母,大写字母转为小写字母。

示例:

str = "HeLLo WoRLd"
print(str.swapcase())

输出结果:

hEllO wOrlD

32. title()函数:将字符串中的每个单词的首字母变成大写

示例:

str = "hello world"
print(str.title())

输出结果:

Hello World

33. upper()函数:将字符串中的所有字母都转为大写字母

示例:

str = "hello world"
print(str.upper())

输出结果:

HELLO WORLD

总结:

Python字符串函数让字符串操作非常方便简单,使用起来可以让你省去很多繁琐的判断和操作。以上的字符串函数中,应该有很多都是很实用的,可以