🌱BeginnerPython Fundamentals

Input and Output

Reading user input with input(), printing output with print(), and formatting numbers.

Overview

Every useful program needs to communicate with the outside world reading data in and displaying results out. In Python, the two primary functions for basic I/O are: **print()** outputs values to the console. It accepts any number of arguments separated by commas, automatically converts them to strings, and by default separates them with a space and ends with a newline. You can customise both behaviours with the sep= and end= parameters. **input()** pauses execution, displays an optional prompt message, and waits for the user to type something and press Enter. It always returns a string, regardless of what the user types. If you need a number, you must convert it explicitly with int() or float(). For formatted numeric output, Python's f-strings support format specifiers using the :format syntax inside {}: {:d} for integers, {:.2f} for floats with 2 decimal places, {:,} for thousands separators, and more.

Key Points

  • •print() converts everything to string and writes to stdout with a newline at the end
  • •print(a, b, c) prints with spaces between by default; change with sep="," etc.
  • •print("hello", end="") suppresses the trailing newline
  • •input() always returns a string convert with int() or float() for numeric input
  • •f-string format specs: f"{pi:.2f}" → "3.14", f"{n:,}" → "1,000,000"
  • •print() can write to a file: print("text", file=open("out.txt", "w"))

Examples

print() with multiple arguments, sep, and end

# Default: space between args, newline at end
print("Hello", "World")         # Hello World

# Custom separator
print("a", "b", "c", sep="-")  # a-b-c

# No trailing newline
print("Loading", end="")
print("...", end="\n")         # Loading...

# Print nothing (just a newline useful for spacing)
print()

input() and type conversion

# In a real script this would pause and wait for user input.
# For demonstration:
name = input("What is your name? ")   # returns a string
age_str = input("How old are you? ")  # also a string!
age = int(age_str)                    # convert to int

print(f"Hello, {name}! In 5 years you will be {age + 5}.")

In exercises and scripts, chain directly: age = int(input("Age: "))

f-string format specifiers

pi = 3.14159265
price = 1234567.89

print(f"{pi:.2f}")        # 3.14  (2 decimal places)
print(f"{pi:.4f}")        # 3.1416 (4 decimal places, rounds)
print(f"{price:,.2f}")    # 1,234,567.89 (comma thousands separator)
print(f"{42:05d}")        # 00042 (zero-padded to width 5)
print(f"{'left':<10}|")  # left       | (left-aligned in 10 chars)
print(f"{'right':>10}|") #      right | (right-aligned)

Common Mistakes

  • Forgetting to convert input() to int/float: age = input("Age: ") gives "25" not 25
  • TypeError when printing non-strings directly with +: use f-strings or str() conversion instead
  • Expecting input() to work in non-interactive contexts (automated scripts, web servers) it's for CLI use

Note

For quick scripts, chain input and conversion on one line: `n = int(input("Enter a number: "))`. This reads, converts, and assigns in a single readable line.

Exercises

Write Python code to solve each exercise. Run it in your browser and check your output.

Loading editor…

Completed: 0 / 5 exercises

Knowledge Check

Question 1 of 4

What does `input("Enter value: ")` always return?