Python Input & Output — print(), input() & Stdin Formatting

🐍 Python 3 🟢 Lesson 12 of 12 📂 Phase 1: Python Basics 📅 2026 Edition
Master console output with print() sep and end parameters, reading user input via input(), handling interactive terminal inputs, and stdin pipelines.
1Mastering the print() Function Parameters

The print() function takes optional keyword arguments that control output formatting:

  • sep="...": The separator string inserted between multiple arguments (default is a single space ' ').
  • end="...": The string appended after the last argument (default is newline '\n'). Setting end="" keeps output on the same line!
  • file=...: Redirects output stream (default is sys.stdout).
2Reading User Input with input()

The input(prompt) function pauses program execution, displays the prompt, and waits for the user to type text and press Enter.

Crucial Rule: input() always returns data as a string (str). If you want numbers, you must explicitly cast: int(input()) or float(input()).

3Interactive Stdin in Our Compiler

When executing Python programs in Our Compiler that call input(), you can type values directly into the interactive Terminal stdin console or pre-enter multiline inputs before clicking Run.

💻 Complete Executable Code Example
import sys

# Custom separator and end characters
print("2026", "08", "14", sep="-")  # Output: 2026-08-14
print("Loading progress: ", end="")
for step in range(1, 4):
    print(f"[{step}/3] ", end="")
print("Done! ✅")

# Reading inputs and calculating result
print("
--- User Input Simulation ---")
mock_name = "Alex"
mock_age_str = "25"

# Simulating input processing
user_name = mock_name
user_age = int(mock_age_str)
birth_year = 2026 - user_age

print(f"Hello, {user_name}!")
print(f"Based on your age ({user_age}), you were born around {birth_year}.")
⚠️ Common Pitfall: Adding Strings and Integers from input()

Writing total = input("Enter num: ") + 5 raises TypeError: can only concatenate str (not "int") to str. Always convert: total = int(input("Enter num: ")) + 5.

💻 Try It Yourself — Hands-on Practice Challenge

Read two numbers and an operator to calculate the arithmetic result.

# Arithmetic Calculator Demo
num1 = 50
num2 = 8
operator = "*"

if operator == "+":
    result = num1 + num2
elif operator == "-":
    result = num1 - num2
elif operator == "*":
    result = num1 * num2
elif operator == "/" and num2 != 0:
    result = num1 / num2
else:
    result = "Invalid Operation"

print(f"🧮 Result of {num1} {operator} {num2} = {result}")
Run This Code in Our Online Compiler →
Frequently Asked Questions (FAQ)

Q: How do you read multiple integers on a single line?

Use string split and list comprehension or map: a, b = map(int, input().split()).

OC
Written by Our Compiler Technical Editorial Team
Reviewed for accuracy & tested on Python 3.12+ runtime · Last updated August 2026