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

Python字符串函数大全:从查找到替换

发布时间:2023-06-04 06:17:12

Python字符串函数在日常编程中经常被使用,可以帮助我们快速地查找、截取、连接、替换字符串等。本文将介绍Python字符串函数大全,包括查找、截取、判断、替换等常用函数,帮助大家更好地利用Python进行字符串处理。

一、Python字符串函数

1、len()函数

len()函数可以返回字符串的长度,即包含多少个字符,使用方法如下:

str = "Hello World"
print(len(str))

输出结果为:11

2、str()函数

str()函数用于将其他数据类型转为字符串类型,使用方法如下:

num = 123
str_num = str(num)
print(type(str_num))

输出结果为: <class 'str'>

3、join()函数

join()函数将一个列表或元组中的元素用指定的分隔符连接成一个字符串,使用方法如下:

list1 = ['hello', 'world', 'python']
sep = ','
str1 = sep.join(list1)
print(str1)

输出结果为: hello,world,python

4、split()函数

split()函数将一个字符串以指定的分隔符分割成多个字符串,并返回一个列表,使用方法如下:

str2 = "hello,world,python"
sep = ','
list2 = str2.split(sep)
print(list2)

输出结果为: ['hello', 'world', 'python']

5、strip()函数

strip()函数用于去除字符串开头和结尾的空格和换行符,使用方法如下:

str3 = "  hello  
"
print(str3.strip())

输出结果为: hello

6、lower()函数

lower()函数将字符串中的大写字母全部转为小写字母,使用方法如下:

str4 = "Hello World"
print(str4.lower())

输出结果为: hello world

7、upper()函数

upper()函数将字符串中的小写字母全部转为大写字母,使用方法如下:

str5 = "Hello World"
print(str5.upper())

输出结果为: HELLO WORLD

8、startswith()函数

startswith()函数用于判断字符串是否以指定的子字符串开头,使用方法如下:

str6 = "Hello World"
prefix = "Hello"
if str6.startswith(prefix):
    print("字符串以%s开头" % prefix)

输出结果为: 字符串以Hello开头

9、endswith()函数

endswith()函数用于判断字符串是否以指定的子字符串结尾,使用方法如下:

str7 = "Hello World"
suffix = "World"
if str7.endswith(suffix):
    print("字符串以%s结尾" % suffix)

输出结果为: 字符串以World结尾

10、find()函数

find()函数用于在字符串中查找指定的子字符串,如果找到了就返回子字符串所在的位置,否则返回-1,使用方法如下:

str8 = "Hello World"
word = "World"
index = str8.find(word)
print("子字符串%s的位置为%d" % (word, index))

输出结果为: 子字符串World的位置为6

11、replace()函数

replace()函数用于在字符串中替换指定的子字符串为另一个字符串,使用方法如下:

str9 = "Hello World"
old_str = "World"
new_str = "Python"
new_str = str9.replace(old_str, new_str)
print(new_str)

输出结果为: Hello Python

12、count()函数

count()函数用于统计字符串中指定子字符串的出现次数,使用方法如下:

str10 = "Hello World"
word = "o"
count = str10.count(word)
print("子字符串%s出现了%d次" % (word, count))

输出结果为: 子字符串o出现了2次

13、isdigit()函数

isdigit()函数用于判断字符串是否全部由数字组成,如果是则返回True,否则返回False,使用方法如下:

str11 = "1234567"
if str11.isdigit():
    print("字符串%s全部由数字组成" % str11)

输出结果为: 字符串1234567全部由数字组成

14、isalpha()函数

isalpha()函数用于判断字符串是否全部由字母组成,如果是则返回True,否则返回False,使用方法如下:

str12 = "HelloWorld"
if str12.isalpha():
    print("字符串%s全部由字母组成" % str12)

输出结果为: 字符串HelloWorld全部由字母组成

15、isalnum()函数

isalnum()函数用于判断字符串是否全部由数字和字母组成,如果是则返回True,否则返回False,使用方法如下:

str13 = "12345abcde"
if str13.isalnum():
    print("字符串%s全部由数字和字母组成" % str13)

输出结果为: 字符串12345abcde全部由数字和字母组成

16、isspace()函数

isspace()函数用于判断字符串是否全部由空格组成,如果是则返回True,否则返回False,使用方法如下:

str14 = "   "
if str14.isspace():
    print("字符串%s全部由空格组成" % str14)

输出结果为: 字符串 全部由空格组成

17、index()函数

index()函数用于在字符串中查找指定的子字符串,如果找到了就返回子字符串所在的位置,否则抛出异常,使用方法如下:

str15 = "Hello World"
word = "World"
index = str15.index(word)
print("子字符串%s的位置为%d" % (word, index))

输出结果为: 子字符串World的位置为6

18、capitalize()函数

capitalize()函数用于将字符串的首字母转为大写字母,使用方法如下:

str16 = "hello world"
new_str = str16.capitalize()
print(new_str)

输出结果为: Hello world

19、title()函数

title()函数用于将字符串中每个单词的首字母都转为大写字母,使用方法如下:

str17 = "hello world python"
new_str = str17.title()
print(new_str)

输出结果为: Hello World Python

20、swapcase()函数

swapcase()函数用于将字符串中的大写字母转为小写字母,小写字母转为大写字母,使用方法如下:

str18 = "Hello World"
new_str = str18.swapcase()
print(new_str)

输出结果为: hELLO wORLD

二、总结

通过本文,我们了解了Python字符串函数的使用方法。这些函数可以帮助我们快速地查找、截取、连接、替换字符串等,使得字符串的处理更加高效。熟练使用字符串函数可以提升编程效率,避免冗长的代码。在实际编程中,需要根据具体的需求灵活应用这些函数。