Python string methods

Python has a set of built-in methods that can be used on strings.

注释:所有字符串方法都返回新值。它们不会更改原始字符串。

Method Description
capitalize() Convert the first character to uppercase.
casefold() 把字符串转换为小写。
center() Return the centered string.
count() Return the number of times the specified value appears in the string.
encode() Return the encoding version of the string.
endswith() If the string ends with the specified value, return true.
expandtabs() Set the tab size of the string.
find() Search for the specified value in the string and return the position where it is found.
format() Format the specified value in the string.
format_map() Format the specified value in the string.
index() Search for the specified value in the string and return the position where it is found.
isalnum() If all characters in the string are alphanumeric, return True.
isalpha() If all characters in the string are in the alphabet, return True.
isdecimal() If all characters in the string are decimal numbers, return True.
isdigit() If all characters in the string are digits, return True.
isidentifier() If the string is an identifier, return True.
islower() If all characters in the string are lowercase, return True.
isnumeric() If all characters in the string are numbers, return True.
isprintable() If all characters in the string are printable, return True.
isspace() If all characters in the string are whitespace characters, return True.
istitle() If the string follows title case rules, return True.
isupper() If all characters in the string are uppercase, return True.
join() 把可迭代对象的元素连接到字符串的末尾。
ljust() 返回字符串的左对齐版本。
lower() 把字符串转换为小写。
lstrip() 返回字符串的左修剪版本。
maketrans() 返回在转换中使用的转换表。
partition() 返回元组,其中的字符串被分为三部分。
replace() 返回字符串,其中指定的值被替换为指定的值。
rfind() 在字符串中搜索指定的值,并返回它被找到的最后位置。
rindex() 在字符串中搜索指定的值,并返回它被找到的最后位置。
rjust() 返回字符串的右对齐版本。
rpartition() 返回元组,其中字符串分为三部分。
rsplit() 在指定的分隔符处拆分字符串,并返回列表。
rstrip() 返回字符串的右边修剪版本。
split() 在指定的分隔符处拆分字符串,并返回列表。
splitlines() 在换行符处拆分字符串并返回列表。
startswith() 如果以指定值开头的字符串,则返回 true。
strip() 返回字符串的剪裁版本。
swapcase() 切换大小写,小写成为大写,反之亦然。
title() 把每个单词的首字符转换为大写。
translate() 返回被转换的字符串。
upper() 把字符串转换为大写。
zfill() 在字符串的开头填充指定数量的 0 值。

注释:所有字符串方法都返回新值。它们不会更改原始字符串。

请在 Python 字符串教程 中学习更多有关字符串的知识。