String Methods Deep Dive
Strings have 40+ built-in methods. Master the most important ones for text processing, validation, and transformation.
15 min•By Priygop Team•Updated 2026
String Methods
String Methods
text = " Hello, World! "
# Case methods
print(text.strip()) # "Hello, World!"
print(text.strip().lower()) # "hello, world!"
print(text.strip().upper()) # "HELLO, WORLD!"
print("hello world".title()) # "Hello World"
print("hello world".capitalize()) # "Hello world"
# Search methods
print("World" in text) # True
print(text.find("World")) # 9
print(text.count("l")) # 3
print(text.startswith(" H")) # True
print(text.endswith("! ")) # True
# Replace & split
print("hello world".replace("world", "Python"))
words = "apple,banana,cherry".split(",")
print(words) # ['apple', 'banana', 'cherry']
joined = " | ".join(words)
print(joined) # "apple | banana | cherry"
# Validation
print("hello123".isalnum()) # True
print("hello".isalpha()) # True
print("12345".isdigit()) # True
print(" ".isspace()) # True
# Practical: clean user input
raw = " aLiCe@Email.COM "
clean = raw.strip().lower()
print(f"Cleaned: {clean}")Tip
Tip
Chain string methods: raw.strip().lower().replace(' ', '_'). Each returns a new string, so you can chain infinitely.
Diagram
Loading diagram…
f-strings > .format() > %. Use = for debug output. :, for thousands separator. :.2f for decimals.
Common Mistake
Warning
Strings are immutable. text.upper() returns a NEW string — it doesn't modify text. You must reassign: text = text.upper().
Practice Task
Note
(1) Clean user input: strip, lowercase. (2) Split a CSV line and join with pipes. (3) Validate if a string is a valid email format.