Python Output & Input
In Python, interacting with users happens through two primary built-in functions: print() for displaying output and input() for receiving user entry.
Python Output: print()
The print() function sends formatted text or data to the console.
Basic Usage
You can pass strings, numbers, variables, or expressions directly into print():
print("Hello, World!")
print(42)
print(10 + 5)
Key Keyword Arguments (sep and end)
By default, print() separates multiple items with a space and ends with a newline character (\n). You can customize this behavior:
sep (Separator)
Defines how multiple arguments are separated.
# Default separator is a space
print("Python", "is", "awesome")
# Output: Python is awesome
# Custom separator
print("2026", "09", "29", sep="-")
# Output: 2026-09-29
print("Apple", "Banana", "Cherry", sep=", ")
# Output: Apple, Banana, Cherry
end (End Character)
Defines what is printed at the end of the print() call (default is \n).
# Printing on the same line using end=""
print("Loading", end="...")
print("Done!")
# Output: Loading...Done!
String Formatting Techniques
Python offers multiple ways to inject variables into output strings:
1. F-Strings (Formatted String Literals) — Recommended
Introduced in Python 3.6, f-strings are fast, readable, and support direct expressions inside {}.
name = "Alex"
age = 25
print(f"User {name} is {age} years old.")
# Output: User Alex is 25 years old.
# inline calculations & methods inside f-strings
print(f"Next year, {name.upper()} will be {age + 1}.")
# Output: Next year, ALEX will be 26.
2. .format() Method
Useful when formatting templates dynamic or pre-defined elsewhere.
item = "Laptop"
price = 999.99
print("The price of the {} is ${:.2f}".format(item, price))
# Output: The price of the Laptop is $999.99
3. % Formatting (Legacy)
An older C-style formatting approach.
name = "Sam"
score = 95.5
print("Student: %s | Score: %.1f" % (name, score))
# Output: Student: Sam | Score: 95.5
Python Input: input()
The input() function pauses execution and waits for the user to type something into the console and press Enter.
Basic Usage
⚠️ Crucial Rule:
input()always returns data as a string (str), even if the user enters digits.
name = input("Enter your name: ")
print(f"Hello, {name}!")
Type Casting User Input
Since input() returns a string, you must explicitly convert (cast) it using int(), float(), etc., if you intend to perform numerical operations.
# Converting input to an integer
age_str = input("Enter your age: ")
age = int(age_str)
print(f"In 5 years, you will be {age + 5} years old.")
Handling Decimals (float)
price = float(input("Enter item price: "))
discount = price * 0.10
print(f"Discounted price: ${price - discount:.2f}")
Handling Errors During Input Conversion
If a user enters non-numeric text when an integer is expected, Python raises a ValueError. Use a try...except block to prevent crashes:
try:
age = int(input("Enter your age: "))
print(f"Valid age recorded: {age}")
except ValueError:
print("Invalid input! Please enter a numerical value.")
Reading Multiple Inputs at Once
You can read space-separated inputs in a single line using .split():
# Reading two values on one line
first_name, last_name = input("Enter your first and last name: ").split()
print(f"First: {first_name}, Last: {last_name}")
# Reading multiple numbers and converting them
numbers = [int(x) for x in input("Enter numbers separated by space: ").split()]
print(f"Sum of numbers: {sum(numbers)}")
Quick Comparison Summary
| Feature | print() |
input() |
|---|---|---|
| Primary Purpose | Display data to the console | Read user entry from the console |
| Return Type | None |
str (Always string) |
| Key Methods/Options | sep, end, F-strings |
.split(), Type casting (int(), float()) |