Input & Output

Difficulty: Beginner

The print() function is Python's primary way of sending output to the console. While it seems simple, print() is quite versatile with its optional parameters. By default, it adds a newline after each call, but you can change this behavior. It can also accept multiple arguments separated by commas, which it joins with spaces.

The print() function accepts two important keyword arguments: sep and end. The sep parameter controls what is placed between multiple arguments (default is a space), and end controls what is appended after all arguments are printed (default is a newline '\n'). These parameters give you fine-grained control over output formatting without needing string concatenation.

The input() function reads a line of text from the user. It always returns a string, regardless of what the user types. You can pass an optional prompt string that is displayed before waiting for input. Since input() always returns a string, you need to use type casting (int(), float()) when you expect numeric input.

F-strings (formatted string literals), introduced in Python 3.6, are the modern and preferred way to embed expressions inside strings. An f-string is prefixed with f or F, and you place expressions inside curly braces {}. The expressions are evaluated at runtime and converted to strings. F-strings support format specifiers for controlling decimal places, alignment, and padding.

Beyond f-strings, Python supports other formatting methods like the .format() method and the older % operator. However, f-strings are the most readable and performant option. They can contain any valid Python expression inside the curly braces, including function calls, arithmetic, and method calls. For complex formatting needs, f-strings support format specifications after a colon, such as {value:.2f} for two decimal places.

Code examples

print() with Multiple Arguments

print("Hello", "World")
print("A", "B", "C", sep="-")
print("No newline", end=" ")
print("Same line")
print("a", "b", "c", sep=", ", end=".\n")

Multiple arguments to print() are separated by spaces by default. The sep parameter changes the separator, and end changes what comes after (default is newline). Combining these gives precise output control.

F-String Basics

name = "Alice"
age = 30
height = 5.678

print(f"Name: {name}")
print(f"{name} is {age} years old")
print(f"Height: {height:.2f} feet")
print(f"Next year: {age + 1}")

F-strings embed expressions inside {}. You can include variables, format specifiers (:.2f for 2 decimal places), and even arithmetic expressions directly inside the braces.

F-String Formatting Options

pi = 3.14159265
big_number = 1000000

print(f"Pi: {pi:.4f}")
print(f"Formatted: {big_number:,}")
print(f"Padded: {42:05d}")
print(f"Left: {'hi':<10}|")
print(f"Right: {'hi':>10}|")
print(f"Center: {'hi':^10}|")

F-strings support rich formatting: :.4f for decimal precision, :, for thousand separators, :05d for zero-padding, and <, >, ^ for left, right, and center alignment with a specified width.

Simulating Input Processing

# In a real program you'd use: name = input("Enter name: ")
# We'll simulate the input values here
name = "Bob"
age_str = "25"

age = int(age_str)
birth_year = 2026 - age

print(f"Hello, {name}!")
print(f"You were born around {birth_year}.")

This simulates a typical input-processing pattern: read strings, convert to the right types, compute results, and display formatted output. In a real program, the values would come from input().

Key points

Concepts covered

print(), input(), f-strings, String formatting, sep and end parameters