Python String functions
Here is a list of commonly used string functions in Python:
len(str)
– Returns the length of the string.str.upper()
– Converts all characters to uppercase.str.lower()
– Converts all characters to lowercase.str.capitalize()
– Capitalizes the first character of the string.str.title()
– Converts the first character of each word to uppercase.str.strip()
– Removes leading and trailing whitespace.str.lstrip()
– Removes leading whitespace (from the left).str.rstrip()
– Removes trailing whitespace (from the right).str.replace(old, new)
– Replaces all occurrences of a substring with another substring.str.find(sub)
– Returns the lowest index of the substring, or-1
if not found.str.index(sub)
– Returns the lowest index of the substring, raises an error if not found.str.count(sub)
– Returns the number of occurrences of a substring.str.startswith(prefix)
– ReturnsTrue
if the string starts with the given prefix.str.endswith(suffix)
– ReturnsTrue
if the string ends with the given suffix.str.split(separator)
– Splits the string into a list based on the separator.str.join(iterable)
– Joins elements of an iterable (like a list) into a string with a separator.str.isalpha()
– ReturnsTrue
if all characters in the string are alphabetic.str.isdigit()
– ReturnsTrue
if all characters in the string are digits.str.isalnum()
– ReturnsTrue
if all characters in the string are alphanumeric.str.isspace()
– ReturnsTrue
if the string contains only whitespace.str.swapcase()
– Converts uppercase to lowercase and vice versa.str.zfill(width)
– Pads the string on the left with zeros to fill the specified width.str.center(width)
– Centers the string with spaces to fill the specified width.str.ljust(width)
– Left-aligns the string with spaces to fill the specified width.str.rjust(width)
– Right-aligns the string with spaces to fill the specified width.str.partition(sep)
– Splits the string into three parts: before, separator, and after.str.rpartition(sep)
– Splits the string into three parts: before, separator, and after, starting from the right.str.splitlines()
– Splits the string at line breaks and returns a list of lines.str.encode(encoding)
– Encodes the string into bytes using the specified encoding.str.decode(encoding)
– Decodes bytes back into a string using the specified encoding.str.casefold()
– Returns a case-insensitive version of the string (similar tolower()
but more aggressive).str.format(*args, **kwargs)
– Formats the string by inserting the values of the arguments.str.format_map(mapping)
– Similar toformat()
, but takes a single mapping argument (like a dictionary).str.expandtabs(tabsize)
– Replaces tabs in the string with spaces; by default, tabsize is 8.str.translate(table)
– Returns a string where each character is mapped based on the provided translation table.str.maketrans(x, y)
– Creates a translation table fortranslate()
.
These string functions allow you to perform various operations on strings such as formatting, splitting, replacing, and checking properties.