Filling in the Blanks (f-strings)
Sometimes you want to create a sentence that includes values from variables.
Doing this with commas or + signs can look messy and hard to read:
name = "Sai"
score = 95
# Messy way:
print("Hello " + name + ", your score is " + str(score) + ".")
To make this clean and simple, Python uses f-strings (Formatted Strings).
1. How f-strings Work
To use an f-string:
- Put the letter
fright before the opening quotation mark of your text. - Put the variable names inside curly brackets
{}directly inside the text.
name = "Sai"
score = 95
# Clean f-string way:
message = f"Hello {name}, your score is {score}."
print(message)
# Output: Hello Sai, your score is 95.
When Python runs this code, it automatically replaces {name} with "Sai" and {score} with 95.
2. Formatting Numbers (Decimal places)
You can also control how numbers look inside f-strings. For example, if you want to show a price with exactly 2 decimal places, you can add :.2f inside the curly brackets:
price = 19.995
quantity = 3
# :.2f tells Python: "Show this number with exactly 2 decimal places"
receipt = f"Total for {quantity} items: ${price * quantity:.2f}"
print(receipt)
# Output: Total for 3 items: $59.98
3. Hands-On Practice Exercises
Try copying these exercises into your hello.py file to see how they work.
Exercise 1: Interactive Receipt Generator
# Ask the user for details
customer_name = input("Customer Name: ")
item_name = input("Purchased Item: ")
price = float(input("Unit Price: "))
# Build the receipt layout using a triple-quoted string
receipt = f"""
=============================
OFFICIAL RECEIPT
=============================
Customer: {customer_name}
Item: {item_name}
Total: ${price:.2f}
=============================
"""
print(receipt)
Exercise 2: Print Server Connection Details
host = "192.168.1.100"
port = "8080"
protocol = "https"
# Use sep="" to avoid spaces between variables
print(protocol, "://", host, ":", port, sep="")
# Output: https://192.168.1.100:8080
Common Interview Questions
Q1: Why are f-strings better than old methods of joining text?
Answer: F-strings are much easier to read, write, and maintain. They are also much faster for the computer to run compared to older Python formatting methods.
Q2: What happens if I forget to convert input() when asking for numbers?
Answer: Python will keep it as text (a string). If you try to do math with it later (like multiplying or adding), Python will either join the text together ("10" + "10" = "1010") or crash with an error because it cannot do math on text.
Module Summary
print(): Shows messages. You can usesep=to change the space between items, andend=to keep text on the same line.input(): Pauses the program to ask the user a question. Always returns their answer as text (String).- Type Casting: Converts text to numbers using
int()orfloat(). - f-strings: A clean way to insert variables directly into text using
f"Hello {name}".
What's Next?
Now that we know how to handle input and output, let's learn how to do math, make comparisons, and write logic using Operators in the next module!