SyntaxStudy
Sign Up
Python Beginner 8 min read

String Methods

Python String Methods

Strings are immutable sequences with powerful built-in methods.

Common Methods

s = "Hello, World!"
print(s.upper())         # HELLO, WORLD!
print(s.lower())         # hello, world!
print(s.strip())         # removes whitespace
print(s.replace("World", "Python"))
print(s.split(", "))     # ['Hello', 'World!']
print(len(s))            # 13
print(s.startswith("Hello"))  # True
print(s.find("World"))   # 7

String Formatting

name = "Alice"
age = 30
print(f"Name: {name}, Age: {age}")
print("Name: {}, Age: {}".format(name, age))
Pro Tip

Strings are zero-indexed: s[0] returns the first character.