Basic F-string
Given: name = "Alex", age = 20 Use an f-string to print: "My name is Alex and I am 20 years old"
F-strings make string formatting easy and readable.
Basic syntax - put f before the string and use {} for variables:
name = "Alice"
age = 25
print(f"My name is {name}") # My name is Alice
print(f"I am {age} years old") # I am 25 years old
You can do calculations inside {}:
a = 5
b = 3
print(f"{a} + {b} = {a + b}") # 5 + 3 = 8
Call methods inside {}:
word = "python"
print(f"Upper: {word.upper()}") # Upper: PYTHON
Given: name = "Alex", age = 20 Use an f-string to print: "My name is Alex and I am 20 years old"
Given: x = 10, y = 7 Use an f-string to print: "10 + 7 = 17" "10 - 7 = 3" "10 * 7 = 70"
Given: word = "python" Use f-string to print: "Original: python" "Upper: PYTHON" "Length: 6"