Python 字符串提供了许多实用的 API(方法)来处理和操作字符串。以下是一些常用的字符串方法及其简单说明:
1. 字符串操作方法
len(s):返回字符串的长度(字符数)。1 2s = "Hello" print(len(s)) # 输出: 5s.lower():将字符串中的所有字符转换为小写。1 2s = "Hello" print(s.lower()) # 输出: "hello"s.upper():将字符串中的所有字符转换为大写。1 2s = "Hello" print(s.upper()) # 输出: "HELLO"s.capitalize():将字符串的首字母大写,其他字符小写。1 2s = "hello" print(s.capitalize()) # 输出: "Hello"s.title():将字符串中的每个单词的首字母大写,其他字母小写。1 2s = "hello world" print(s.title()) # 输出: "Hello World"s.strip():移除字符串两端的空白字符(或其他指定字符)。1 2s = " hello " print(s.strip()) # 输出: "hello"s.lstrip():移除字符串左侧的空白字符。1 2s = " hello" print(s.lstrip()) # 输出: "hello"s.rstrip():移除字符串右侧的空白字符。1 2s = "hello " print(s.rstrip()) # 输出: "hello"
2. 查找与替换
s.find(sub):返回子字符串sub在字符串s中第一次出现的索引,如果没有找到则返回-1。1 2s = "hello world" print(s.find("world")) # 输出: 6s.rfind(sub):返回子字符串sub在字符串s中最后一次出现的索引,如果没有找到则返回-1。1 2s = "hello world, world" print(s.rfind("world")) # 输出: 13s.index(sub):与find()类似,但如果sub不存在会引发ValueError异常。1 2s = "hello world" print(s.index("world")) # 输出: 6s.replace(old, new):将字符串中的所有old子字符串替换为new子字符串。1 2s = "hello world" print(s.replace("world", "Python")) # 输出: "hello Python"
3. 判断类型
s.isdigit():判断字符串是否只包含数字字符。1 2s = "12345" print(s.isdigit()) # 输出: Trues.isalpha():判断字符串是否只包含字母字符。1 2s = "hello" print(s.isalpha()) # 输出: Trues.isalnum():判断字符串是否只包含字母和数字字符。1 2s = "hello123" print(s.isalnum()) # 输出: Trues.isspace():判断字符串是否只包含空白字符(空格、换行等)。1 2s = " " print(s.isspace()) # 输出: Trues.startswith(prefix):判断字符串是否以指定前缀prefix开头。1 2s = "hello world" print(s.startswith("hello")) # 输出: Trues.endswith(suffix):判断字符串是否以指定后缀suffix结尾。1 2s = "hello world" print(s.endswith("world")) # 输出: True
4. 字符串拆分与连接
s.split(sep):按照指定的分隔符sep拆分字符串,返回一个列表。如果不指定sep,默认按照空白字符拆分。1 2s = "hello world" print(s.split()) # 输出: ['hello', 'world']s.splitlines():按行分割字符串,返回一个列表。1 2s = "hello\nworld" print(s.splitlines()) # 输出: ['hello', 'world']sep.join(iterable):用sep连接一个可迭代对象中的所有字符串,返回一个拼接后的字符串。1 2words = ['hello', 'world'] print(" ".join(words)) # 输出: "hello world"
5. 字符串对齐
s.center(width):将字符串s居中,并使用空格填充至指定宽度width。1 2s = "hello" print(s.center(10)) # 输出: " hello "s.ljust(width):将字符串s左对齐,并使用空格填充至指定宽度width。1 2s = "hello" print(s.ljust(10)) # 输出: "hello "s.rjust(width):将字符串s右对齐,并使用空格填充至指定宽度width。1 2s = "hello" print(s.rjust(10)) # 输出: " hello"
6. 其他常用方法
s.count(sub):返回子字符串sub在字符串s中出现的次数。1 2s = "hello world" print(s.count("o")) # 输出: 2s.zfill(width):返回一个字符串,长度为width,原字符串右对齐,左侧用零填充。1 2s = "42" print(s.zfill(5)) # 输出: "00042"s.partition(sep):将字符串s按sep分为三部分,返回一个三元组(head, sep, tail)。1 2s = "hello world" print(s.partition(" ")) # 输出: ('hello', ' ', 'world')s.rpartition(sep):与partition类似,但从字符串末尾开始搜索sep。1 2s = "hello world world" print(s.rpartition(" ")) # 输出: ('hello world', ' ', 'world')
这些方法涵盖了常见的字符串操作,可以帮助你处理和操作字符串数据。根据具体需求选择合适的方法即可。
💬 评论