Python: String Operations
What you will learn
Common string methods and f-strings for formatting.
text = " Hello, World! "
text.lower() # " hello, world! "
text.upper() # " HELLO, WORLD! "
text.strip() # "Hello, World!" (removes whitespace)
text.split(",") # [" Hello", " World! "]
text.replace("World", "Python") # " Hello, Python! "
" ".join(["a", "b"]) # "a b"
len(text) # 16 (includes spaces)
f-strings (Python 3.6+)
name = "Alice"
age = 25
print(f"{name} is {age} years old.")
print(f"Next year, {name} will be {age + 1}.")
f-strings evaluate expressions inside { } and insert the result into the string. They are the preferred way to format strings in modern Python.
Quick check below!