Python String Methods

Dealing with strings is a very common task in programming. Python makes it very easy by providing us tons of useful functions. The following are the list of all built-in methods for strings.

String Methods

Method Description
capitalize Return a copy of the string with the first character capitalized the rest lowercased
casefold Return a casefolded copy of the string. An aggressive lower() method, used for caseless matching of strings
center Return a centered string in a given length
count Return the number of occurrences of a substring in a string
encode Return an encoded version of the string as a bytes object
endswith Return True if the string ends with the specified substring, otherwise False
expandtabs Replace tab characters with spaces
find Return the lowest index where the substring is found in the string
format Perform a string formatting operation
format_map Similar to format but mapping is not copied to dict and used directly
index Similar to find except it raises ValueError when the substring is not found
isalnum Return True if all characters are alphanumeric
isalpha Return True is all characters are alphabet
isascii Return True if all characters are ASCII
isdecimal Return True if all characters are decimal characters
isdigit Return True if all characters are digits
isidentifier Return True if the string is a valid identifier
islower Return True if all cased characters are in lowercase
isnumeric Return True if all characters are numeric characters
isprintable Return True if all characters are printable
isspace Return True if all characters in the string are whitespace
istitle Return True if the string is titlecased
isupper Return True if all the cased characters are in upper case
join Return a string by concatenating the strings in iterable
ljust Return the left-justified string in a given width
lower Return a copy of the string with all the lowercased characters
lstrip Return a copy of the string with leading characters removed
maketrans Return a translation table usable for translate
partition Split the string at the first occurrence. Return a 3-tuple - left part, separator, right part
replace Return a copy of the string with occurrences of old replaced by new
rfind Return the highest index in the string where the substring is found
rindex Similar to rfind except it raises ValueError when the substring is not found
rjust Return the string right-justified in a string of given width
rpartition Split the string at last occurence. Return a 3-tuple - left part, separator, right part
rsplit Return a list of the words in the string using a delimiter string
rstrip Return a copy of the string with trailing characters removed
split Return a list of the words in the string using a separator
splitlines Return a list of the lines in the string, breaking at line boundaries
startswith Return True if the string starts with the given substring
strip Return a copy of the string with the leading and trailing characters removed
swapcase Return a copy of the string by swapping the case of the string
title Return a titlecased version of the string
translate Return a copy of the string by mapping all the characters by given translation table
upper Return a copy of the string with all the cased characters uppercased
zfill Return a copy of the string left filled with ASCII '0' for a given length

 

0 Comments

Join discussion:

Login