Python String Methods

Python provides a set of built-in methods to manipulate and operate on strings. These methods make it easy to perform common tasks such as searching, splitting, and formatting strings. Below is a list of some commonly used string methods, along with their descriptions and links to detailed guides for each method.

Python String Methods Table

Method Description
format() Formats specified values in a string.
split() Splits a string into a list where each word is a list item.
join() Joins the elements of an iterable into a single string, separated by the string that called the method.
strip() Removes any leading and trailing characters (space is the default).
replace() Replaces a specified phrase with another specified phrase.
lower() Converts a string into lowercase.
upper() Converts a string into upper case.
count() Returns the number of times a specified value appears in the string.
find() Searches the string for a specified value and returns the position of where it was found.
rsplit() Splits a string into a list, starting from the right.
splitlines() Splits a string at line breaks and returns a list.
center() Returns a centered string.
zfill() Fills the string with a specified number of 0 values at the beginning.
startswith() Returns true if the string starts with the specified value.
endswith() Returns true if the string ends with the specified value.
translate() Returns a string where some specified characters are replaced with the character described in a dictionary or mapping table.
casefold() Converts a string into lowercase.
capitalize() Converts the first character to upper case.
title() Converts the first character of each word to upper case.
expandtabs() Sets the tab size of the string.
islower() Returns true if all characters in the string are lowercase.
isupper() Returns true if all characters in the string are upper case.
isnumeric() Returns true if all characters in the string are numeric.
isdecimal() Returns true if all characters in the string are decimals.
isdigit() Returns true if all characters in the string are digits.
isprintable() Returns true if all characters in the string are printable.
isspace() Returns true if all characters in the string are whitespaces.
istitle() Returns true if the string follows the rules of a title.
encode() Returns an encoded version of the string.
maketrans() Returns a translation table to be used in translations.
format_map() Formats specified values in a string using a dictionary.
index() Searches the string for a specified value and returns the position of where it was found.
isalnum() Returns true if all characters in the string are alphanumeric.
isalpha() Returns true if all characters in the string are in the alphabet.
isidentifier() Returns true if the string is a valid identifier.
ljust() Returns a left-justified version of the string.
partition() Returns a tuple where the string is parted into three parts.
rfind() Searches the string for a specified value and returns the last position of where it was found.
rindex() Searches the string for a specified value and returns the last position of where it was found.
rjust() Returns a right justified version of the string.
rpartition() Returns a tuple where the string is parted into three parts.
rstrip() Removes any trailing characters (space is the default).
swapcase() Swaps cases, the lower case becomes the upper case and vice versa.
title() Converts the first character of each word to upper case.
translate() Returns a string where some specified characters are replaced with the character described in a dictionary or mapping table.
upper() Converts a string into upper case.
zfill() Fills the string with a specified number of 0 values at the beginning.

For more detailed information on each method, refer to the official Python documentation.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top