Python String Methods

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

Note:All string methods return a new value. They do not change the original string.

Method Description
capitalize() Convert the first character to uppercase.
casefold() Convert the string to lowercase.
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 values in the string.
format_map() Format the specified values 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() Concatenate the elements of an iterable object to the end of the string.
ljust() Return the left-aligned version of the string.
lower() Convert the string to lowercase.
lstrip() Return the left-trimmed version of the string.
maketrans() Return the translation table used in the conversion.
partition() Return a tuple where the string is divided into three parts.
replace() Return a string in which the specified value is replaced with the specified value.
rfind() Search for the specified value in the string and return the last position where it is found.
rindex() Search for the specified value in the string and return the last position where it is found.
rjust() Return the right-aligned version of the string.
rpartition() Return a tuple where the string is divided into three parts.
rsplit() Split the string at the specified delimiter and return a list.
rstrip() Return the right-trimmed version of the string.
split() Split the string at the specified delimiter and return a list.
splitlines() Split the string at the newline character and return a list.
startswith() Return true if the string starts with the specified value.
strip() Return the truncated version of the string.
swapcase() Switch the case, lowercase becomes uppercase and vice versa.
title() Convert the first character of each word to uppercase.
translate() Return the converted string.
upper() Convert the string to uppercase.
zfill() Fill the beginning of the string with the specified number of 0 values.

Note:All string methods return a new value. They do not change the original string.

Please Python String Tutorial Learn more about strings in Chinese.