Python函数库中的字符串函数
Python是一种高级编程语言,它有许多内置函数和模块,其中包括一些用于处理字符串的函数。字符串是Python中的基本数据类型之一,它可以包含字母、数字、符号等字符序列,可以使用字符串函数中的函数来操作、转换、格式化和搜索字符串数据。
1. capitalize()
capitalize()函数将字符串中的 个字符转换为大写,其他字符转换为小写。
示例:
string = "hello, world!" print(string.capitalize())
输出:
Hello, world!
2. casefold()
casefold()函数将字符串中的所有字符转换为小写,并对一些特殊情况进行处理,例如对于某些字符会转换为多个小写字符。该函数可以用于比较字符串时忽略大小写。
示例:
string1 = "HELLO, WORLD!" string2 = "?" print(string1.casefold()) print(string2.casefold())
输出:
hello, world! ss
3. center()
center()函数将字符串居中,并在两边填充指定字符,不指定字符则默认用空格填充。
示例:
string = "hello" print(string.center(10)) print(string.center(10, '*'))
输出:
hello **hello***
4. count()
count()函数用于统计某个字符或子字符串在字符串中出现的次数。
示例:
string = "hello, world!"
print(string.count('l'))
print(string.count('ol'))
输出:
3 1
5. encode()
encode()函数将字符串编码为二进制数据,并可以指定编码格式。
示例:
string = "hello, world!"
print(string.encode())
print(string.encode('base64'))
输出:
b'hello, world!' b'aGVsbG8sIHdvcmxkIQ== '
6. endswith()
endswith()函数用于检查字符串是否以指定的后缀结尾。
示例:
string = "hello, world!"
print(string.endswith('orld!'))
print(string.endswith('world'))
输出:
True False
7. find()
find()函数用于搜索字符串中的子字符串,如果找到则返回子字符串的索引值,否则返回-1。
示例:
string = "hello, world!"
print(string.find('world'))
print(string.find('Python'))
输出:
7 -1
8. format()
format()函数用于格式化字符串。它用一组花括号表示占位符,可以将变量、字符串、表达式作为参数传入,然后将它们替换掉占位符后输出。
示例:
name = "Tom"
age = 20
print("My name is {}, and I am {} years old.".format(name, age))
输出:
My name is Tom, and I am 20 years old.
9. index()
index()函数与find()函数类似,但是如果没有找到子字符串,则会抛出ValueError异常。
示例:
string = "hello, world!"
print(string.index('world'))
print(string.index('Python'))
输出:
7 Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: substring not found
10. isalnum()
isalnum()函数检查字符串是否只包含字母和数字字符,不包含空格或其他特殊字符。
示例:
string1 = "hello123" string2 = "hello 123" print(string1.isalnum()) print(string2.isalnum())
输出:
True False
11. isalpha()
isalpha()函数检查字符串是否只包含字母字符,不包含数字、空格或其他特殊字符。
示例:
string1 = "hello" string2 = "hello 123" print(string1.isalpha()) print(string2.isalpha())
输出:
True False
12. isdigit()
isdigit()函数检查字符串是否只包含数字字符。
示例:
string1 = "123" string2 = "hello 123" print(string1.isdigit()) print(string2.isdigit())
输出:
True False
13. islower()
islower()函数检查字符串是否只包含小写字母。
示例:
string1 = "hello" string2 = "Hello" print(string1.islower()) print(string2.islower())
输出:
True False
14. isupper()
isupper()函数检查字符串是否只包含大写字母。
示例:
string1 = "HELLO" string2 = "Hello" print(string1.isupper()) print(string2.isupper())
输出:
True False
15. join()
join()函数将一组字符串连接起来,并使用指定的分隔符分隔。
示例:
list1 = ['hello', 'world']
list2 = ['1', '2', '3']
print("-".join(list1))
print("*".join(list2))
输出:
hello-world 1*2*3
16. lstrip()
lstrip()函数用于删除字符串左侧的空格或指定字符。
示例:
string1 = " hello"
string2 = "**hello**"
print(string1.lstrip())
print(string2.lstrip('*'))
输出:
hello hello**
17. partition()
partition()函数用于将字符串划分为三部分,以指定的分隔符为准。
示例:
string = "hello,world"
print(string.partition(','))
输出:
('hello', ',', 'world')
18. replace()
replace()函数用于替换字符串中的指定字符或子字符串。
示例:
string = "hello, world!"
print(string.replace('world', 'Python'))
输出:
hello, Python!
19. rfind()
rfind()函数与find()函数类似,但是从右侧开始搜索子字符串。
示例:
string = "hello, world!"
print(string.rfind('l'))
print(string.rfind('Python'))
输出:
10 -1
20. rstrip()
rstrip()函数用于删除字符串右侧的空格或指定字符。
示例:
string1 = "hello "
string2 = "**hello**"
print(string1.rstrip())
print(string2.rstrip('*'))
输出:
hello **hello
总结:
Python函数库中的字符串函数可以帮助我们更便捷地操作、转换、格式化和搜索字符串数据。本文介绍了20个常用的字符串函数,包括capitalize()、casefold()、center()、count()、encode()、endswith()、find()、format()、index()、isalnum()、isalpha()、isdigit()、islower()、isupper()、join()、lstrip()、partition()、replace()、rfind()、rstrip()等。
