› Module 3: Input and Output › 3.1 print() and Formatted Output
M O D U L E

Module 3: Input and Output

The two doors between your program and the outside world: printing formatted results out, and reading user data in.

3.2 input() for Reading User Data

The input(prompt) function displays the given prompt text, pauses the entire program, and waits until the person at the keyboard types something and presses Enter.

Here is the detail that catches almost every beginner at least once:

Whatever the user types is always returned as a string — even if they type "25", Python hands your program back the three characters "2", "5", not the number 25.

The fix (from the Guide):

If you need a number, you must explicitly convert it yourself: age = int(input("Enter age: ")). Forgetting this is one of the most common early bugs, because comparing the string "25" to the number 25 with == will silently return False instead of raising an obvious error.

Reading Multiple Inputs on a Single Line

In coding interviews and competitive programming platforms, test cases often supply multiple space-separated numbers on one line. The standard idiom is combining split() with map():

# Reading space-separated integers on a single line:
a, b, c = map(int, input("Enter 3 numbers: ").split())
print(f"Sum = {a + b + c}")

Defensive Input Validation

If a user types non-numeric text like "twenty" when passed to int(), Python raises a ValueError. Production code handles this gracefully with a loop and try-except:

while True:
    try:
        age = int(input("Please enter your age: "))
        break # Valid integer received!
    except ValueError:
        print("Invalid input! Please enter digits only.")
MODULE COMPREHENSIVE QUIZ

Module 3 Final Quiz

Test your understanding across all 2 topics of this module with interview-grade questions, tricky edge cases, and code output predictions.

Take Module 3 Final Quiz →
Yukti Mascot