String Operations in Python
From the Python cheat sheet ยท Strings & Formatting ยท verified Jul 2026
String Operations
Common string methods and operations.
python
s = "Hello, World!"
s.lower() # "hello, world!"
s.upper() # "HELLO, WORLD!"
s.strip() # Remove whitespace
s.split(", ") # ["Hello", "World!"]
s.replace("World", "Python")
s.startswith("Hello") # True
"World" in s # True๐ก Strings are immutable โ methods return new strings, they don't modify in place
โก Use "in" for substring checks โ it's more Pythonic than .find()
๐ .find() returns -1 if not found; .index() raises ValueError
๐ข Use s[::-1] to reverse a string with slicing
stringsmethods