Input, Output and String Formatting
Week 1 — Python Basics Revision
Introduction
Programs need to communicate with the people using them. Output shows results to users, and input lets users provide data. Together, they make programs interactive rather than just running silently in the background.
The print() Function
The print() function displays text to the screen. You can print multiple values at once, and control how they are separated and ended.
print("Hello, World!")
print("Name:", "Alice", "Age:", 22)
print("One", "Two", "Three", sep=" - ")
print("Loading", end="...")
print("Done!")
Hello, World!
Name: Alice Age: 22
One - Two - Three
Loading...Done!
Getting User Input
The input() function pauses the program and waits for the user to type something. It always returns a string, so you must convert it if you need a number.
name = input("What is your name? ")
print("Hello,", name)
age = int(input("Your age: "))
print("Next year you will be", age + 1)
F-Strings (String Formatting)
F-strings are the modern, clean way to embed variables inside strings. Just put an f before the quotes and wrap variables in curly braces.
name = "Alice"
age = 22
gpa = 3.856
print(f"Name: {name}")
print(f"Age: {age}")
print(f"GPA: {gpa:.2f}") # 2 decimal places
print(f"GPA: {gpa:.1f}") # 1 decimal place
Name: Alice
Age: 22
GPA: 3.86
GPA: 3.9
Formatting Numbers
F-strings give you fine control over how numbers are displayed — decimal places, thousands separators, and zero-padding.
price = 1299.5
quantity = 1000000
print(f"Price: ${price:.2f}") # $1299.50
print(f"Population: {quantity:,}") # 1,000,000
print(f"Padded: {42:05d}") # 00042
Price: $1299.50
Population: 1,000,000
Padded: 00042
A Complete Example
Here is a small program that combines input, type conversion, and f-string formatting to create a simple receipt.
# Simple receipt
item = input("Item name: ")
price = float(input("Price: $"))
qty = int(input("Quantity: "))
total = price * qty
print(f"\n--- Receipt ---")
print(f"Item: {item}")
print(f"Price: ${price:.2f}")
print(f"Quantity: {qty}")
print(f"Total: ${total:.2f}")
--- Receipt ---
Item: Notebook
Price: $4.99
Quantity: 3
Total: $14.97
How This Connects to OOP
In OOP, classes often have a special __str__ method that defines how an object prints itself. It uses the same f-string formatting you just learned. When you call print(my_object), Python automatically calls that method.
Best Practices
- Always use f-strings for formatting — they are cleaner than string concatenation
- Convert input with
int()orfloat()before using it in calculations - Use
.2ffor money values to always show two decimal places
Common Mistakes
- Forgetting that
input()returns a string — useint(input(...))for numbers - Using
+to join strings and numbers — use f-strings instead - Not handling bad input (user types "abc" when a number is expected — the program crashes)
Practice Exercises
- Ask for the user's name and age, then print:
"Hello [name], you are [age] years old." - Ask for a temperature in Celsius and convert to Fahrenheit using
F = C * 9/5 + 32. Display the result with 1 decimal place. - Create a formatted receipt: ask for 3 items and their prices, display each item and the total.
Summary
print()displays output, with optionalsepandendparameters.input()reads user input as a string — convert it for math.- F-strings (
f"...") are the cleanest way to format output. - Format specifiers like
.2fand,control number display.
You know how to store data and communicate with users. Now let us learn how to make decisions with if, elif, and else.