Input, Output & String Formatting
Python's built-in print() and input() functions handle console I/O. Modern Python uses f-strings for clean string formatting.
# Basic I/O
name = input("Enter your name: ")
print("Hello,", name)
# F-strings (Python 3.6+)
age = 20
gpa = 3.875
print(f"{name} is {age} years old. GPA: {gpa:.2f}")
# Output: Alice is 20 years old. GPA: 3.88
# Format spec mini-language
print(f"{42:08b}") # 00101010 (binary, width 8)
print(f"{1234567:,}") # 1,234,567 (thousands separator)
print(f"{0.1234:.2%}") # 12.34% (percentage)
# Multi-line & separator
print("a", "b", "c", sep="-", end="!\n") # a-b-c!
input() always returns a string. Convert with int() or float() before arithmetic: age = int(input("Age: "))
Knowledge Check
What does input() always return?
The f-string f'{3.14159:.2f}' produces
How do you print items separated by commas in print()?
f'{255:#x}' formats 255 as
To read a number from the user, which is correct?