Python strings come with a variety of built-in methods that allow you to perform different operations on them. Here's a list of all common string methods available in Python, along with examples for each. This list is not exhaustive but covers the most frequently used methods.
- Description: Converts the first character of the string to uppercase.
- Example:
s = 'hello world'
print(s.capitalize()) # Output: 'Hello world'
- Description: Converts string to lowercase, designed for caseless matching.
- Example:
s = 'HELLO World'
print(s.casefold()) # Output: 'hello world'
- Description: Returns a centered string of length width.
- Example:
s = 'hello'
print(s.center(20, '*')) # Output: '*******hello********'
- Description: Returns the number of occurrences of a substring in the string.
- Example:
s = 'hello world'
print(s.count('o')) # Output: 2
- Description: Returns an encoded version of the string as a bytes object.
- Example:
s = 'hello world'
print(s.encode()) # Output: b'hello world'
- Description: Returns True if the string ends with the specified suffix.
- Example:
s = 'hello world'
print(s.endswith('world')) # Output: True
- Description: Replaces tabs in a string with the specified number of spaces.
- Example:
s = 'hello\tworld'
print(s.expandtabs(4)) # Output: 'hello world'
- Description: Returns the lowest index in the string where substring sub is found.
- Example:
s = 'hello world'
print(s.find('world')) # Output: 6
- Description: Formats the string using format specifiers.
- Example:
s = 'Hello, {}'
print(s.format('world')) # Output: 'Hello, world'
- Description: Formats the string using a dictionary.
- Example:
s = 'Hello, {name}'
print(s.format_map({'name': 'world'})) # Output: 'Hello, world'
- Description: Like find(), but raises ValueError when the substring is not found.
- Example:
s = 'hello world'
print(s.index('world')) # Output: 6
- Description: Returns True if all characters in the string are alphanumeric.
- Example:
s = 'hello123'
print(s.isalnum()) # Output: True
- Description: Returns True if all characters in the string are alphabetic.
- Example:
s = 'hello'
print(s.isalpha()) # Output: True
- Description: Returns True if all characters in the string are decimal characters.
- Example:
s = '123'
print(s.isdecimal()) # Output: True
- Description: Returns True if all characters in the string are digits.
- Example:
s = '123'
print(s.isdigit()) # Output: True
- Description: Returns True if the string is a valid identifier according to Python syntax.
- Example:
s = 'hello_world'
print(s.isidentifier()) # Output: True
- Description: Returns True if all cased characters in the string are lowercase.
- Example:
s = 'hello world'
print(s.islower()) # Output: True
- Description: Returns True if all characters in the string are numeric characters.
- Example:
s = '123'
print(s.isnumeric()) # Output: True
- Description:
Returns True if all characters in the string are printable or the string is empty.
- Example:
s = 'hello world'
print(s.isprintable()) # Output: True
- Description: Returns True if all characters in the string are whitespace.
- Example:
s = ' '
print(s.isspace()) # Output: True
- Description: Returns True if the string is a title-cased string.
- Example:
s = 'Hello World'
print(s.istitle()) # Output: True
- Description: Returns True if all cased characters in the string are uppercase.
- Example:
s = 'HELLO WORLD'
print(s.isupper()) # Output: True
- Description: Joins the elements of an iterable to the string.
- Example:
s = '-'
print(s.join(['hello', 'world'])) # Output: 'hello-world'
- Description: Returns a left-justified string of length width.
- Example:
s = 'hello'
print(s.ljust(10, '*')) # Output: 'hello*****'
- Description: Converts all characters in the string to lowercase.
- Example:
s = 'HELLO WORLD'
print(s.lower()) # Output: 'hello world'
- Description: Returns a copy of the string with leading characters removed.
- Example:
s = ' hello world'
print(s.lstrip()) # Output: 'hello world'
- Description: Splits the string at the first occurrence of sep.
- Example:
s = 'hello world'
print(s.partition(' ')) # Output: ('hello', ' ', 'world')
- Description: Returns a string where all occurrences of old have been replaced by new.
- Example:
s = 'hello world'
print(s.replace('world', 'Python')) # Output: 'hello Python'
- Description: Returns the highest index in the string where substring sub is found.
- Example:
s = 'hello world, hello Python'
print(s.rfind('hello')) # Output: 13
- Description: Like rfind(), but raises ValueError when the substring is not found.
- Example:
s = 'hello world, hello Python'
print(s.rindex('hello')) # Output: 13
- Description: Returns a right-justified string of length width.
- Example:
s = 'hello'
print(s.rjust(10, '*')) # Output: '*****hello'
- Description: Splits the string at the last occurrence of sep.
- Example:
s = 'hello world, hello Python'
print(s.rpartition(' ')) # Output: ('hello world, hello', ' ', 'Python')
- Description: Splits the string at the separator sep and returns a list of strings.
- Example:
s = 'hello world hello Python'
print(s.rsplit(' ', 1)) # Output: ['hello world hello', 'Python']
- Description: Returns a copy of the string with trailing characters removed.
- Example:
s = 'hello world '
print(s.rstrip()) # Output: 'hello world'
- Description: Splits the string at the separator sep and returns a list.
- Example:
s = 'hello world hello Python'
print(s.split()) # Output: ['hello', 'world', 'hello', 'Python']
- Description: Splits the string at line breaks and returns a list of lines.
- Example:
s = 'hello\nworld'
print(s.splitlines()) # Output: ['hello
', 'world']
- Description: Returns True if the string starts with the specified prefix.
- Example:
s = 'hello world'
print(s.startswith('hello')) # Output: True
- Description: Returns a copy of the string with leading and trailing characters removed.
- Example:
s = ' hello world '
print(s.strip()) # Output: 'hello world'
- Description: Converts uppercase characters to lowercase and vice versa.
- Example:
s = 'Hello World'
print(s.swapcase()) # Output: 'hELLO wORLD'
- Description: Returns a title-cased version of the string.
- Example:
s = 'hello world'
print(s.title()) # Output: 'Hello World'
- Description: Returns a copy of the string in which each character has been mapped through a given translation table.
- Example:
# Translation table to replace 'h' with 'j'
trans = str.maketrans('h', 'j')
s = 'hello world'
print(s.translate(trans)) # Output: 'jello world'
- Description: Converts all characters in the string to uppercase.
- Example:
s = 'hello world'
print(s.upper()) # Output: 'HELLO WORLD'
- Description: Pads the string on the left with zeros to fill width.
- Example:
s = '42'
print(s.zfill(5)) # Output: '00042'
These methods are part of the core Python language and provide powerful tools for manipulating strings in various ways.