Python: Basic Input and Output
In this tutorial, you will learn how to display output to the screen and take input from the user using Python. These are essential building blocks for creating interactive programs.
Learning Objectives
-
Use
print()to display messages or variable values -
Use
input()to get data from the user - Understand how Python handles input as strings
-
Convert user input to numeric data types using
int()andfloat() - Combine input and output for interactive programs
Output in Python — print()
The print() function is used to display output on the screen, such as messages, numbers, or variable values.
Print a simple message
Display a greeting to the user.
print("Hello Python!")
Explanation: This line outputs the text "Hello Python!" to the console.
Print multiple values
The simplest way to print multiple values in Python is by separating them with commas inside the print() function. By default, each value will be displayed on the same line, separated by a single space.
name = "Alice"
age = 25
print("Name:", name, "Age:", age)
# Output: Name: Alice Age: 25
Explanation:
- "Name:" and "Age:" are string literals.
- name and age are variables.
- The print() function separates them using spaces by default.
- Output: Name: Alice Age: 25.
Customize Output with sep and end
When using Python’s print() function, you’re not limited to the default formatting. Two powerful parameters—sep and end—allow you to customize how your output is displayed. This can be especially useful when you need cleaner formatting, specific output structures, or more control over line breaks.
Using sep (Separator)
The sep parameter allows you to customize how multiple items are separated in the output.
Example 1: Default Separator (Space)
print("Python", "is", "awesome")
# Output: Python is awesome
Example 2: Custom Separator
To change the separator, assign a string to sep.
print("2025", "07", "18", sep="-")
# Output: 2025-07-18print("Name", "Age", "Location", sep=" | ")
# Output: Name | Age | Location
Example 3: No Separator
print("Python", "Rocks", sep="")
# Output: PythonRocks
Using end (End Character)
The end parameter defines what will be printed at the end of the line. By default, it adds a newline (\n), but you can change it to anything else.
Example 1: Print Without Newline
print("Loading", end="...")
print("Done!")
# Output: Loading...Done!
Example 2: Custom End Character
print("Line 1", end=" | ")
print("Line 2", end=" | ")
print("Line 3")
# Output: Line 1 | Line 2 | Line 3
Combining sep and end
You can use both sep and end together for full control of the output:
print("A", "B", "C", sep="-", end=".\n")
# Output: A-B-C.print("Item1", "Item2", sep=", ", end=" <-- End of Line\n")
# Output: Item1, Item2 <-- End of Line
Input in Python — input()
The input() function in Python allows you to interact with the user by accepting input via the keyboard. It is one of the fundamental building blocks for creating dynamic, interactive programs. It always returns the input as a string.
Purpose of input()
- Make programs interactive: Ask the user for data instead of hard coding values.
- Adapt program behavior: Change the flow or output based on user input.
- Collect runtime information: e.g., names, ages, commands, choices, numbers, etc.
Syntax:
input(prompt)
- prompt (optional): A string that is printed to the console to prompt the user.
- Returns: A string containing the user input.
Basic Example:
name = input("Enter your name: ")
print("Hello,", name)
Explanation:
- The user sees the prompt: Enter your name:
- Once the user types his/her name (assume the user entered his name Alice) and presses enter, the input is stored in the variable name.
- The program then prints Hello, Alice.
Input is always a String
No matter what the user types, input() returns a string. If you want to treat the input as a number (integer or float), you need to convert it using type casting.
Example: Reading Numbers
num = input("Enter a number: ")
print("You entered:", num)
print("Type of num:", type(num))
Suppose the user enters 10, then the output will be:
You entered: 10
Type of num: <class 'str'>
Even though the user entered 10, it’s stored as a string, not an integer. You cannot perform an arithmetic operation on it yet.
Type Conversion with int() and float()
To use numbers entered by the user in calculations, you must convert the string to a number.
Example: Convert to Integer
age = int(input("Enter your age: "))
print("You will be", age + 1, "next year.")
Explanation:
- input() collects the string input.
- int() converts the string to an integer.
- We then perform arithmetic.
Example: Convert to Float
Use float() for decimal numbers.
price = float(input("Enter the price: "))
print("Price with tax:", price * 1.15)
Example:
Write a program that asks the user for two numbers and prints their average:
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
average = (num1 + num2) / 2
print("Average is:", average)
Quick Reference Table
| Function | Description |
|---|---|
print()
|
Displays output to the screen |
input()
|
Accepts input from the user (string only) |
int()
|
Converts a string to an integer |
float()
|
Converts a string to a floating-point number |
Tips and Best Practices
-
Always prompt clearly when using
input() - Use type conversion to handle numeric input safely
-
Practice by combining
input()andprint()in small interactive scripts
Summary
In this lesson, you learned how to:
-
Use
print()to output data -
Use
input()to receive user input - Convert input values to numeric types
- Build simple interactive Python programs