Complete reference of all Python string methods with syntax and examples
Converts all characters to uppercase
text = "hello world"
result = text.upper()
print(result) # HELLO WORLDConverts all characters to lowercase
text = "HELLO WORLD"
result = text.lower()
print(result) # hello worldCapitalizes the first character and lowercases the rest
text = "hello world"
result = text.capitalize()
print(result) # Hello worldConverts the first character of each word to uppercase
text = "hello world"
result = text.title()
print(result) # Hello WorldSwaps uppercase to lowercase and vice versa
text = "Hello World"
result = text.swapcase()
print(result) # hELLO wORLDConverts string to lowercase (more aggressive than lower())
text = "HELLO"
result = text.casefold()
print(result) # helloReturns the lowest index of substring, or -1 if not found
substring (required), start (optional), end (optional)
text = "Hello World"
index = text.find("World")
print(index) # 6
not_found = text.find("Python")
print(not_found) # -1Returns the highest index of substring, or -1 if not found
substring (required), start (optional), end (optional)
text = "Hello World World"
index = text.rfind("World")
print(index) # 12Like find() but raises ValueError if not found
substring (required), start (optional), end (optional)
text = "Hello World"
index = text.index("World")
print(index) # 6Like rfind() but raises ValueError if not found
substring (required), start (optional), end (optional)
text = "Hello World World"
index = text.rindex("World")
print(index) # 12Returns the number of occurrences of substring
substring (required), start (optional), end (optional)
text = "Hello World World"
count = text.count("World")
print(count) # 2Returns True if string starts with the specified prefix
prefix (required), start (optional), end (optional)
text = "Hello World"
result = text.startswith("Hello")
print(result) # TrueReturns True if string ends with the specified suffix
suffix (required), start (optional), end (optional)
text = "Hello World"
result = text.endswith("World")
print(result) # TrueReturns True if all characters are alphabetic
text1 = "Hello"
print(text1.isalpha()) # True
text2 = "Hello123"
print(text2.isalpha()) # FalseReturns True if all characters are digits
text1 = "12345"
print(text1.isdigit()) # True
text2 = "123abc"
print(text2.isdigit()) # FalseReturns True if all characters are alphanumeric
text1 = "Hello123"
print(text1.isalnum()) # True
text2 = "Hello 123"
print(text2.isalnum()) # FalseReturns True if all characters are whitespace
text1 = " "
print(text1.isspace()) # True
text2 = " a "
print(text2.isspace()) # FalseReturns True if all cased characters are lowercase
text1 = "hello"
print(text1.islower()) # True
text2 = "Hello"
print(text2.islower()) # FalseReturns True if all cased characters are uppercase
text1 = "HELLO"
print(text1.isupper()) # True
text2 = "Hello"
print(text2.isupper()) # FalseReturns True if string is titlecased
text1 = "Hello World"
print(text1.istitle()) # True
text2 = "Hello world"
print(text2.istitle()) # FalseSplits string into a list using separator
separator (optional, default: whitespace), maxsplit (optional)
text = "Hello World Python"
words = text.split()
print(words) # ['Hello', 'World', 'Python']
csv = "apple,banana,orange"
fruits = csv.split(",")
print(fruits) # ['apple', 'banana', 'orange']Splits string from the right
separator (optional), maxsplit (optional)
text = "apple,banana,orange"
result = text.rsplit(",", 1)
print(result) # ['apple,banana', 'orange']Splits string at line breaks
keepends (optional, default: False)
text = "Hello\nWorld\nPython"
lines = text.splitlines()
print(lines) # ['Hello', 'World', 'Python']Joins elements of an iterable with separator
iterable (required)
words = ["Hello", "World", "Python"]
result = " ".join(words)
print(result) # Hello World Python
result2 = "-".join(words)
print(result2) # Hello-World-PythonSplits string into 3-tuple: (before, separator, after)
separator (required)
text = "Hello-World"
result = text.partition("-")
print(result) # ('Hello', '-', 'World')Like partition() but searches from the right
separator (required)
text = "Hello-World-Python"
result = text.rpartition("-")
print(result) # ('Hello-World', '-', 'Python')Removes leading and trailing characters (default: whitespace)
chars (optional)
text = " Hello World "
result = text.strip()
print(result) # "Hello World"
text2 = "***Hello***"
result2 = text2.strip("*")
print(result2) # "Hello"Removes leading characters (default: whitespace)
chars (optional)
text = " Hello World"
result = text.lstrip()
print(result) # "Hello World"Removes trailing characters (default: whitespace)
chars (optional)
text = "Hello World "
result = text.rstrip()
print(result) # "Hello World"Removes prefix if present (Python 3.9+)
prefix (required)
text = "HelloWorld"
result = text.removeprefix("Hello")
print(result) # "World"Removes suffix if present (Python 3.9+)
suffix (required)
text = "HelloWorld"
result = text.removesuffix("World")
print(result) # "Hello"Replaces occurrences of old with new
old (required), new (required), count (optional)
text = "Hello World World"
result = text.replace("World", "Python")
print(result) # Hello Python Python
result2 = text.replace("World", "Python", 1)
print(result2) # Hello Python WorldTranslates string using translation table
table (required)
table = str.maketrans("aeiou", "12345")
text = "hello world"
result = text.translate(table)
print(result) # h2ll4 w4rldCreates a translation table
x (required), y (optional), z (optional)
table = str.maketrans("abc", "123")
text = "abcdef"
result = text.translate(table)
print(result) # 123defCenters string in a field of given width
width (required), fillchar (optional, default: space)
text = "Hello"
result = text.center(11)
print(result) # " Hello "
result2 = text.center(11, "*")
print(result2) # "***Hello***"Left-aligns string in a field of given width
width (required), fillchar (optional)
text = "Hello"
result = text.ljust(10)
print(result) # "Hello "Right-aligns string in a field of given width
width (required), fillchar (optional)
text = "Hello"
result = text.rjust(10)
print(result) # " Hello"Pads string with zeros on the left
width (required)
number = "42"
result = number.zfill(5)
print(result) # "00042"Formats string using placeholders
*args, **kwargs
template = "Hello {}, you are {} years old"
result = template.format("Alice", 25)
print(result) # Hello Alice, you are 25 years old
template2 = "Hello {name}, age: {age}"
result2 = template2.format(name="Bob", age=30)
print(result2) # Hello Bob, age: 30Formats string using a dictionary
mapping (required)
template = "Hello {name}, age: {age}"
data = {"name": "Alice", "age": 25}
result = template.format_map(data)
print(result) # Hello Alice, age: 25Encodes string to bytes
encoding (optional, default: 'utf-8'), errors (optional)
text = "Hello World"
encoded = text.encode()
print(encoded) # b'Hello World'
encoded_utf16 = text.encode('utf-16')
print(encoded_utf16)Expands tabs to spaces
tabsize (optional, default: 8)
text = "Hello\tWorld"
result = text.expandtabs(4)
print(result) # "Hello World"