Code Reference

Input and Output Basics

Python · Reference cheat sheet

Input and Output Basics

Python · Reference cheat sheet


📋 Overview

Programs talk to the outside world through output (showing results) and input (reading data). Beginners usually start with print for output and input for reading a line of text from the keyboard.

🔧 Core concepts

ToolDirectionNotes
print(*values)OutConverts values to strings; default ends with newline
input(prompt)InAlways returns a str (even for numbers you type)
f-stringsOutf"Hello \{name\}" embeds expressions
sep / endOutControl separators and line endings
CastingInUse int(...), float(...) after reading text

Standard streams: stdout (normal output), stderr (errors), stdin (input).

💡 Examples

Basic print and input:

name = input("What is your name? ")
print("Hello,", name)
print(f"Welcome, {name}!")

Convert typed text to numbers:

raw = input("Enter a whole number: ")
n = int(raw)
print(n, "squared is", n * n)

print options:

print("a", "b", "c", sep="-")
print("Loading", end="...")
print(" done")

Safe parse with a try/except:

raw = input("Price: ").strip()
try:
    price = float(raw)
except ValueError:
    print("Please enter a number like 9.99")
else:
    print(f"You entered {price:.2f}")

⚠️ Pitfalls

  • input returns strings: "5" + "1" is "51", not 6.
  • int("3.14") fails — use float first or validate.
  • EOF in redirected input can raise EOFError.
  • For real apps, prefer logging over scattered print debugging.

On this page