Are your print lines messy with endless quotation marks, variables, and plus signs? In this video, we dissect F-Strings (Formatted Strings) in Python.
You'll learn why f-strings are the modern standard for text formatting, how to embed variables directly using curly braces without manual data type casting, and how to execute calculations or string methods directly inside the braces.
📝 CODE WRITTEN IN THIS VIDEO:
--------------------------------------
Old, messy way (Concatenation)
name = "Alice"
age = 25
print("Hello " + name + ", you are " + str(age) + " years old.")
New, clean way (F-Strings)
print(f"Hello {name}, you are {age} years old.")
Substituted strings & prices
item = "Burger"
price = 99
print(f"{item}: ${price}")
Executing math inside f-strings
print(f"Next year you will be {age + 1}.")
🧠 BONUS CHALLENGES:
--------------------------------------
Test your f-string formatting skills! Comment your solutions below.
Challenge 1:
What is the output of this code?
score = 90
grade = "A"
print(f"Grade {grade}: {score - 10}%")
Options:
A) Grade A: 80%
B) Grade A: score - 10%
C) Error
Challenge 2:
What is the output of this code?
item = "apple"
print(f"I bought an {item.capitalize() * 2}")
Options:
A) I bought an AppleApple
B) I bought an appleapple
C) I bought an Apple Apple
#python #programming #coding #pythontutorial #learnpython #compsci