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

More Python tasks

Back to the full Python cheat sheet