Reference library
Python code samples
Easy snippets you can copy, study, and run in the browser editor.
F-string formatting
Embed variables and expressions inside readable f-strings.
name = "Ada"
score = 97.5
label = f"{name} scored {score:.1f}%"
print(label)
print(f"Next year: {2026 + 1}")
Split and join words
Turn a sentence into tokens, then rebuild it with a custom separator.
raw = "python split join example"
words = raw.split()
print(words)
joined = "-".join(words)
print(joined)
csv_line = "red,green,blue"
colors = csv_line.split(",")
print(" | ".join(colors))
Strip and normalize text
Remove stray whitespace and compare user input reliably.
messy = " Hello, Python! \n"
clean = messy.strip()
print(repr(clean))
user = " YES "
if user.strip().lower() == "yes":
print("Confirmed")
Enumerate with index
Loop with both index and value — cleaner than manual counters.
tasks = ["read docs", "write code", "run tests"]
for i, task in enumerate(tasks, start=1):
print(f"{i}. {task}")
Zip two lists
Pair items from parallel lists and iterate them together.
names = ["Ada", "Grace", "Katherine"]
scores = [98, 95, 100]
for name, score in zip(names, scores):
print(f"{name}: {score}")
print(list(zip(names, scores)))
Function with default argument
Define a reusable greet helper with an optional prefix parameter.
def greet(name, prefix="Hello"):
return f"{prefix}, {name}!"
print(greet("Python"))
print(greet("World", prefix="Hi"))
Try/except ValueError
Convert user input to int inside try/except and show a friendly message.
raw = "42"
try:
value = int(raw)
print(f"Parsed: {value}")
except ValueError:
print(f"Could not parse {raw!r} as an integer.")
Browse by section
Each section groups closely related Python snippets.