Input & Output

Input & Output in Python: In almost all programs, you need a way to communicate with the user.

Concept Explanation

In almost all programs, you need a way to communicate with the user. Output is how the program talks back to you (usually via the screen), and Input is how you give data or instructions to the program (usually via the keyboard).

In Python, we handle these two essential tasks using built-in functions: print() for Output, and input() for Input. They form the foundation of interactive scripts.


Syntax or Core Structure

Output

print("Score:", 100)
# f-strings (Python 3.6+)
name = "Alice"
print(f"Hello, {name}")

Input

Always returns a string.

name = input("Enter your name: ")
age = int(input("Enter your age: ")) # Type casting

Code Example #1 (Basic)

Here is a complete loop of asking the user for their name and then greeting them.

# 1. Ask for input
user_name = input("What is your character's name? ")

# 2. Output a greeting
print("Welcome to the game, " + user_name + "!")

Code Example #2 (Intermediate)

Let's build a simple calculator that requires inputting two numbers and calculating their sum. Notice how we use f-strings to format the final output elegantly.

print("--- Simple Addition Calculator ---")
first_number = input("Enter the first number: ")
second_number = input("Enter the second number: ")

# We must cast the string inputs to integers before doing math!
total = int(first_number) + int(second_number)

# Using an f-string to inject the variables directly into the text
print(f"The sum of ${first_number} and ${second_number} is ${total}")

Real World Example

Imagine a checkout script in a terminal-based store.

print("Checkout System Initialized.")
item_price = 15.50
quantity_str = input(f"How many items (at $${item_price} each) are you purchasing? ")

# Convert to integer
quantity = int(quantity_str)
total_cost = item_price * quantity

print(f"Your total comes out to $${total_cost}.")
payment = input("Type 'PAY' to confirm your transaction: ")

if payment == "PAY":
    print("Transaction successful! Printing receipt...")

Common Mistakes

  1. Forgetting Input Returns Strings: The number one mistake beginners make is trying to do math on the result of input(). If you run age = input("Age?") and type 10, the value of age is "10" (a string), not 10. age + 5 will crash. You must cast it: int(age) + 5.
  2. Missing F-String 'f': When trying to interpolate variables like print("Hello {name}"), if you forget the f at the beginning of the string, Python will literally print the word "{name}" instead of substituting the variable.

Best Practices

  • Use F-Strings: F-strings (added in Python 3.6) are the cleanest, fastest, and most modern way to format strings in Python. Prefer them over "String " + variable concatenations.
  • Clear Prompts: Always put a space at the end of your input() prompt string (e.g., input("Name: ") instead of input("Name:")). It makes the terminal cursor visually separated from the text, improving user experience.

Practice Exercises

  1. Interactive Greeting: Write a script that asks the user for their first name, their favorite color, and prints a sentence combining both.
  2. Age Calculator: Ask the user for the year they were born. Calculate their approximate age in the current year and print it.
  3. Receipt Printer: Ask the user for an item name and its price. Print a formatted receipt using an f-string: Receipt: 1x [ItemName] - $[Price].
  4. Fix the Crash: num = input("Pick a number: ") followed by print(num * 2). If the user types 5, it prints 55. Fix the code so it prints 10.