Python String Methods Cheatsheet
A complete reference for Python's built-in string methods.
Essential String Methods
| Method | Description | Example |
|---|---|---|
upper() |
Convert to uppercase |
"hello".upper() → "HELLO"
|
lower() |
Convert to lowercase |
"HELLO".lower() → "hello"
|
strip() |
Remove whitespace |
" hi ".strip() → "hi"
|
split() |
Split into list |
"a,b".split(",") → ["a","b"]
|
join() |
Join list into string |
",".join(["a","b"]) → "a,b"
|
replace() |
Replace substring |
"hello".replace("l","r") → "herro"
|
find() |
Find index of substring |
"hello".find("ll") → 2
|
startswith() |
Check prefix |
"hello".startswith("he") → True
|
endswith() |
Check suffix |
"hello".endswith("lo") → True
|
format() |
String formatting |
"{} {}".format("a","b") → "a b"
|
count() |
Count occurrences |
"hello".count("l") → 2
|
isdigit() |
Check if digits |
"123".isdigit() → True
|
isalpha() |
Check if letters |
"abc".isalpha() → True
|
title() |
Title case |
"hello world".title() → "Hello World"
|
Code Examples
# Common string operations
text = " Hello, World! "
# Cleaning
clean = text.strip() # "Hello, World!"
lower = clean.lower() # "hello, world!"
upper = clean.upper() # "HELLO, WORLD!"
# Searching
idx = clean.find("World") # 7
count = clean.count("l") # 3
has_hello = clean.startswith("Hello") # True
# Transforming
replaced = clean.replace("World", "Python") # "Hello, Python!"
words = clean.split(", ") # ["Hello", "World!"]
joined = " - ".join(words) # "Hello - World!"
# Checking
print("123".isdigit()) # True
print("abc".isalpha()) # True
print("abc123".isalnum()) # True
# F-strings (modern Python)
name = "Python"
version = 3.13
print(f"{name} {version:.1f}") # Python 3.1
Pro Tips
- Use
f"{value:.2f}"for float formatting -
str.join()is faster than+for concatenation - Use
"in"operator:"hello" in text
Follow me for more Python cheatsheets! 🐍
Follow for more Python content!
🛠️ Recommended Tool
If you found this useful, check out Content Creator Ultimate Bundle (Save 33%) — $29.99 and designed for developers like you.
Get instant access to our best-selling AI Dev Boost, HTML Landing Page Templates, AI Prompts for Developers, and Python Automation Scripts Pack, perfect for content creators and marketers looking to elevate their game. This bundle is a must-have for anyone looking to create stunning content, build high-converting landing pages, and drive real results. With these tools, you'll be able to create engaging content, build beautiful landing pages, and boost your online presence.
Top comments (0)