input() & Output Formatting
Your programs so far have been monologues. Today they become conversations — reading from the user, and printing output that looks polished.
input() — asking the user
name = input("What is your name? ")
print(f"Hello, {name}!")Run it, and the program pauses at the prompt, waits, and whatever the user types (before Enter) becomes the value:
What is your name? Aarav
Hello, Aarav!The text inside the parentheses is the prompt — displayed to the user as the question. Always end prompts with a space or the cursor touches your text.
The golden rule (last warning, promise)
input() returns a string. Always. Even when it looks like a number:
height = input("Height in cm: ") # user types 170
print(height + 5) # TypeError — "170" + 5 is nonsenseThe fix you now know:
height = int(input("Height in cm: "))And for decimals — weight, prices, temperatures — float():
weight = float(input("Weight in kg: "))Multiple inputs, one line
name, age = input("Name: "), int(input("Age: "))More readable and very common in competitive coding — split one line of input:
# user types: Aarav 16
name, age = input("Enter name and age: ").split()
age = int(age)
print(f"{name} will be {age + 1} next year").split() chops the input at spaces into a list — you'll formalize lists in Module 5, but you can use this pattern today.
print() — the full toolkit
Separate values with commas (spaces added automatically):
name, score = "Aarav", 95
print("Name:", name, "| Score:", score) # Name: Aarav | Score: 95Custom separators and endings:
print("A", "B", "C", sep="-") # A-B-C
print("Loading", end="... ") # no newline after
print("done!") # continues on the same linef-strings with formatting mini-language:
price = 49.98765
print(f"Price: ₹{price:.2f}") # ₹49.99 — 2 decimal places
print(f"{0.856:.0%}") # 86% — percentages
print(f"{42:5d}") # 42 — padded to width 5The {value:.2f} syntax reads: "insert value, format as fixed-point with 2 decimals." Three formats cover 95% of real use: .2f (money/measurements), .0% (percentages), :5d (alignment).
A complete conversation program
Everything from this module in one script:
name = input("Your name: ")
age = int(input("Your age: "))
height = float(input("Height in meters: "))
bmi = weight = 0 # placeholder for later modules
print(f"\n--- Profile ---")
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Adult: {age >= 18}")
print(f"\nNext birthday, {name}, you turn {age + 1}!")Sample run:
Your name: Aarav
Your age: 16
Height in meters: 1.7
--- Profile ---
Name: Aarav
Age: 16
Adult: False
Next birthday, Aarav, you turn 17!Notice age >= 18 printing False — comparisons are values too, and f-strings display them happily.
Common Errors & Fixes
end="" somewhere and forgot; every print ends with a newline unless you say otherwise.✅ Checkpoint
print("Hi", end="") do differently? *(Skips the newline — next print continues the same line)*0.07268 as a percentage with 1 decimal. *(f"{0.07268:.1%}" → 7.3%)*Module 3 checkpoint reached! You can now store, calculate, convert, converse, and display. Next: 🧪 Practice — eight exercises that lock it all in.