Courses/Python Mastery/Module 3: Variables & Data Types
Module 3 · Lesson 620 minBeginner

input() & Output Formatting

Lesson goal
Talk to your user: read input, print beautifully with sep, end, and f-strings.

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

code
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:

code
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:

code
height = input("Height in cm: ")    # user types 170
print(height + 5)                   # TypeError — "170" + 5 is nonsense

The fix you now know:

code
height = int(input("Height in cm: "))

And for decimals — weight, prices, temperatures — float():

code
weight = float(input("Weight in kg: "))

Multiple inputs, one line

code
name, age = input("Name: "), int(input("Age: "))

More readable and very common in competitive coding — split one line of input:

code
# 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.

Separate values with commas (spaces added automatically):

code
name, score = "Aarav", 95
print("Name:", name, "| Score:", score)    # Name: Aarav | Score: 95

Custom separators and endings:

code
print("A", "B", "C", sep="-")        # A-B-C
print("Loading", end="... ")         # no newline after
print("done!")                       # continues on the same line

f-strings with formatting mini-language:

code
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 5

The {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:

code
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:

code
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

  • `TypeError` right after an input — the golden rule. Convert before math.
  • `ValueError: invalid literal for int()` — the user typed letters. For now, rerun and type properly; the polite fix arrives with try/except in Module 8.
  • Output on one weird line — you used end="" somewhere and forgot; every print ends with a newline unless you say otherwise.

  • ✅ Checkpoint

  • Build one line that asks for two numbers and prints their sum. *(a = int(input()); b = int(input()); print(a + b))*
  • What does print("Hi", end="") do differently? *(Skips the newline — next print continues the same line)*
  • Format 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.