Lesson 4 of 30
Input and Output
Using print(), input(), and formatting output with f-strings.
Printing Output
The print() function writes text to the console. You can pass multiple arguments separated by commas:
print("Name:", "Alice") # Name: Alice
print("Score:", 95, "/ 100") # Score: 95 / 100
f-Strings (Formatted String Literals)
f-strings (available since Python 3.6) let you embed expressions directly inside strings:
name = "Bob"
age = 30
print(f"My name is {name} and I am {age} years old.")
# My name is Bob and I am 30 years old.
pi = 3.14159
print(f"Pi ≈ {pi:.2f}") # Pi ≈ 3.14
Reading Input
input() pauses execution and waits for the user to type something. It always returns a string:
user_name = input("Enter your name: ")
print(f"Hello, {user_name}!")
Converting Input to Numbers
Because input() returns a string, you must convert it before doing arithmetic:
age_str = input("Enter your age: ")
age = int(age_str)
print(f"In 10 years you will be {age + 10}.")
Practical Example — Simple Calculator
a = float(input("Enter first number: "))
b = float(input("Enter second number: "))
print(f"Sum: {a + b}")
print(f"Difference: {a - b}")
print(f"Product: {a * b}")
print(f"Quotient: {a / b:.4f}")