字符串常用方法

发布时间 2023-03-30 20:48:00作者: 我不知道取什么名字好

1.capitalize() 方法:将字符串的首字母大写。

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

输出:Hello, world

2.casefold() 方法:将字符串转换为小写并删除所有大小写特有的字符,使字符串可以比较。

str = "Hello, WORLD"
print(str.casefold())

输出:hello, world

3.center() 方法:将字符串居中对齐,并使用指定字符填充不足的空间。

str = "hello"
print(str.center(10, '*'))

输出:hello

4.count() 方法:返回字符串中指定子字符串的出现次数。

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

输出:3

5.encode() 方法:将字符串转换为指定编码的字节序列。

str = "hello, world"
print(str.encode('utf-8'))

输出:b'hello, world'

6.endswith() 方法:判断字符串是否以指定子字符串结尾。

str = "hello, world"
print(str.endswith('ld'))

输出:True

7.expandtabs() 方法:将字符串中的制表符转换为指定数量的空格。

str = "hello,\tworld"
print(str.expandtabs(4))

输出:hello, world

8.find() 方法:查找字符串中指定子字符串的位置。

str = "hello, world"
print(str.find('l'))

输出:2

9.format() 方法:将参数格式化为指定格式的字符串。

x = 3
y = 5
print("The value of x is {} and y is {}".format(x, y))

输出:The value of x is 3 and y is 5

10.format_map() 方法:使用字典格式化字符串。

person = {'name': 'Alice', 'age': 25}
print("My name is {name} and I am {age} years old".format_map(person))

输出:My name is Alice and I am 25 years old

11.index() 方法:查找字符串中指定子字符串的位置。与 find() 方法相似,但子字符串不存在时会抛出异常。

str = "hello, world"
print(str.index('l'))

输出:2

12.isalnum() 方法:检查字符串是否只包含字母数字字符。

str = "hello123"
print(str.isalnum())

输出:True

13.isalpha() 方法:检查字符串是否只包含字母字符。

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

输出:True

14.isdecimal() 方法:检查字符串是否只包含十进制数字字符。

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

输出:True

15.isdigit() 方法:检查字符串是否只包含数字字符。

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

输出:True

16.isidentifier() 方法:检查字符串是否是一个有效的标识符。

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

输出:True

17.islower() 方法:检查字符串是否只包含小写字母。

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

输出:True

18.isnumeric() 方法:检查字符串是否只包含数字字符。

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

输出:True

19.isprintable() 方法:检查字符串是否只包含可打印的字符。

str = "hello, world"
print(str.isprintable())

输出:True

20.isspace() 方法:检查字符串是否只包含空格字符。

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

输出:True

21.istitle() 方法:检查字符串中的单词是否都以大写字母开头并且其余字母都是小写字母。

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

输出:True

22.isupper() 方法:检查字符串是否只包含大写字母。

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

输出:True

23.join() 方法:将序列中的元素连接为一个字符串。

my_list = ["apple", "banana", "cherry"]
print("-".join(my_list))

输出:apple-banana-cherry

24.ljust() 方法:将字符串左对齐,并使用指定字符填充不足的空间。

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

输出:hello*****

25.lower() 方法:将字符串转换为小写。

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

输出:hello

26.lstrip() 方法:删除字符串左侧的空格字符。

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

输出:hello

27.partition() 方法:查找子字符串,并将其拆分为三部分:前缀、子字符串、后缀。

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

输出:('hello', ',', ' world')

28.replace() 方法:将字符串中的子字符串替换为另一个字符串。

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

输出:hell0, w0rld

29.rfind() 方法:从右侧开始查找字符串中指定子字符串的位置。

str = "hello, world"
print(str.rfind('l'))

输出:10

30.rindex() 方法:从右侧开始查找字符串中指定子字符串的位置。与 rfind() 方法相似,但子字符串不存在时会抛出异常。

str = "hello, world"
print(str.rindex('l'))

输出:10

31.rjust() 方法:将字符串右对齐,并使用指定字符填充不足的空间。

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

输出:*****hello

32.rpartition() 方法:从右边开始查找子字符串,并将其拆分为三部分:前缀、子字符串、后缀。

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

输出:('hello, world', '', '')

33.rsplit() 方法:将字符串从右侧开始拆分为指定数量的子字符串,并返回这些子字符串作为列表。

str = "hello, world"
print(str.rsplit(',', 1))

输出:['hello', ' world']

34.rstrip() 方法:从字符串的右侧删除空格字符。

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

输出:hello

35.split() 方法:将字符串拆分为指定数量的子字符串,并将这些子字符串作为列表返回。默认情况下,字符串根据空格进行拆分,但是可以指定分隔符。

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

输出:['hello', ' world']

36.splitlines() 方法:将字符串拆分为行,并将这些行作为列表返回。

str = "hello,\nworld"
print(str.splitlines())

输出:['hello,', 'world']

37.startswith() 方法:检查字符串是否以指定子字符串开头。

str = "hello, world"
print(str.startswith('he'))

输出:True

38.strip() 方法:从字符串两侧删除空格字符。

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

输出:hello

39.swapcase() 方法:交换字符串中大写字母和小写字母的位置。

str = "Hello, World"
print(str.swapcase())

输出:hELLO, wORLD

40.title() 方法:将字符串中的每个单词的第一个字母变为大写。

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

输出:Hello, World

41.translate() 方法:使用指定的翻译表进行字符串转换。

str = "hello, world"
table = str.maketrans('hw', '12')
print(str.translate(table))

输出:12ello, 1orld

42.upper() 方法:将字符串转换为大写。

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

输出:HELLO

43.zfill() 方法:将字符串填充到指定长度,并在左侧使用零字符('0')进行填充。

str = "123"
print(str.zfill(5))

输出:00123