Python provides a wide range of string methods that allow you to manipulate and work with strings. Here are some commonly used string methods in Python:
capitalize()
: Converts the first character of the string to uppercase and converts all other characters to lowercase.casefold()
: Converts the string to lowercase, suitable for case-insensitive comparisons.center()
: Returns a centered string by padding it with specified characters.count()
: Returns the number of occurrences of a substring in the string.encode()
: Returns the encoded version of the string.find()
: Searches for a specified substring within the string and returns the index of its first occurrence.index()
: Similar tofind()
, but raises an exception 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.isdigit()
: Checks if all characters in the string are digits.islower()
: Checks if all characters in the string are lowercase.isnumeric()
: Checks if all characters in the string are numeric.isspace()
: Checks if all characters in the string are whitespace.istitle()
: Checks if the string follows the rules of a titlecased string.isupper()
: Checks if all characters in the string are uppercase.join()
: Concatenates elements of an iterable (e.g., a list) into a single string using the specified separator.lstrip()
: Removes leading whitespace from the string.rstrip()
: Removes trailing whitespace from the string.partition()
: Splits the string at the first occurrence of a specified separator and returns a tuple.splitlines()
: Splits the string at line breaks and returns a list of lines.swapcase()
: Converts lowercase characters to uppercase and vice versa.title()
: Converts the first character of each word in the string to uppercase.zfill()
: Pads the string with zeros (0) on the left to a specified width.