Python String Methods
Python provides a robust suite of built-in string methods that simplify the process of working with text data. These methods return new values and leave the original string unchanged, ensuring immutability and safety in your operations.
Overview of Common Python String Methods
Below is a comprehensive reference of string methods available in Python, useful for formatting, analyzing, and transforming string data. These methods are essential for anyone learning Python or dealing with text manipulation.
Method | Description |
---|---|
capitalize() | Converts the first character of the string to uppercase. |
casefold() | Converts all characters in the string to lowercase, more aggressive than lower(). |
center() | Returns the string centered in a field of a given width. |
count() | Counts how many times a substring appears in the string. |
encode() | Returns an encoded version of the string in the specified encoding. |
endswith() | Checks if the string ends with the specified suffix. |
expandtabs() | Replaces tab characters with spaces, using a specified tab size. |
find() | Searches for a substring and returns the first occurrence index. |
format() | Formats specified values and inserts them into placeholders in the string. |
format_map() | Similar to format(), but uses a mapping for substitutions. |
index() | Like find(), but raises an error if the substring is not found. |
isalnum() | Checks if all characters in the string are alphanumeric. |
isalpha() | Checks if all characters in the string are alphabetic. |
isascii() | Returns True if all characters are ASCII characters. |
isdecimal() | Checks if all characters are decimal numbers. |
isdigit() | Returns True if the string consists of digits only. |
isidentifier() | Returns True if the string is a valid identifier. |
islower() | Checks if all alphabetic characters are lowercase. |
isnumeric() | Returns True if the string contains numeric characters only. |
isprintable() | Returns True if all characters are printable. |
isspace() | Checks if the string contains only whitespace characters. |
istitle() | Returns True if the string is title-cased. |
isupper() | Returns True if all characters are uppercase. |
join() | Concatenates an iterable of strings with the string as a separator. |
ljust() | Left-justifies the string in a field of given width. |
lower() | Converts all characters to lowercase. |
lstrip() | Removes whitespace or specified characters from the beginning. |
maketrans() | Creates a translation table for use with translate(). |
partition() | Splits the string into three parts around a separator. |
replace() | Replaces a substring with another value. |
rfind() | Finds the last occurrence of a substring. |
rindex() | Like rfind() but raises an error if not found. |
rjust() | Right-justifies the string in a field of given width. |
rpartition() | Splits the string into three parts, searching from the right. |
rsplit() | Splits the string from the right by a separator. |
rstrip() | Removes trailing whitespace or specified characters. |
split() | Splits the string by a specified separator. |
splitlines() | Splits the string at line boundaries. |
startswith() | Checks if the string begins with a specified value. |
strip() | Removes leading and trailing whitespace or characters. |
swapcase() | Swaps the case of each character in the string. |
title() | Converts the first character of each word to uppercase. |
translate() | Returns a string where each character is mapped through a translation table. |
upper() | Converts all characters to uppercase. |
zfill() | Pads the string with zeros on the left to fill a width. |
This guide is maintained and enhanced by Devyra, your go-to source for practical and modern programming tutorials.