Strings
f-strings, common string methods, splitting and joining, and regular expressions with re.
Runs in Zerethon IDE · Python 3.12
Official Python documentation ↗
main.py
import re
name = "Ada Lovelace"
print(f"{name} has {len(name)} characters")
print(name.upper(), name.lower(), name[:3])
print(" padded ".strip() + "|")
print("a,b,c".split(","), "-".join(["x", "y", "z"]))
print(name.replace("Ada", "Augusta"), name.startswith("Ada"), "love" in name.lower())
print(f"{3.14159:.2f} {42:>5} {7:03d}")
text = "Order 12 shipped, order 7 pending"
print(re.findall(r"\d+", text))
match = re.search(r"order (\d+) pending", text)
print(match.group(1) if match else "no match")
Output
Ada Lovelace has 12 characters ADA LOVELACE ada lovelace Ada padded| ['a', 'b', 'c'] x-y-z Augusta Lovelace True True 3.14 42 007 ['12', '7'] 7
- Strings cannot be changed in place; methods such as
replace()return a new string. - Use raw strings (
r"\d+") for regular expressions so backslashes are kept. re.search()returnsNonewhen nothing matches — check before calling.group().