|
| Home |
|---|
Python input and output are fundamental parts of Python programming. Input allows a program to receive information from a user, while output allows the program to display information and results.
Python provides simple built-in functions for handling basic input and
output. The two functions beginners use most often are
input() for receiving user input and print()
for displaying output.
Understanding Python input and output is essential because many programs need to communicate with users. Whether you are creating a simple calculator, a student grading program, a quiz, or a larger application, you will often need to accept data and display results.
In this tutorial, you will learn how to use input() and
print(), accept text and numeric input, convert input into
different data types, format output, use f-strings, control spaces and
line breaks, and handle common input and output mistakes.
Input is information provided to a program. In a
beginner Python program, input commonly comes from the keyboard when a
user enters information using the input() function.
Output is information produced by a program. Python
commonly displays output on the screen using the print()
function.
The basic relationship can be represented as:
Input → Processing → Output
For example, a calculator program may ask the user for two numbers, perform a calculation and then display the answer.
The Python print() function is used to display text,
numbers, variables and the results of expressions on the screen.
It is one of the most commonly used built-in functions in Python.
The simplest form of Python output is:
print("Hello Python")
Hello Python
The text inside the quotation marks is displayed on the screen.
The print() function can display numbers as well as text.
Numbers do not need quotation marks.
print(10)
print(25.5)
print(100 + 50)
10
25.5
150
Python evaluates the expression 100 + 50 before displaying
the result.
The print() function can display the value stored in a
variable.
name = "Alice"
age = 25
print(name)
print(age)
Alice
25
The values stored in name and age are displayed
when the program runs.
Python allows you to pass multiple values to print().
By default, Python separates the values with a space.
name = "Alice"
age = 25
print(name, age)
Alice 25
You can also combine text and variables in the same
print() statement.
name = "Alice"
age = 25
print("Name:", name)
print("Age:", age)
Name: Alice
Age: 25
The Python input() function is used to receive information
from the user through the keyboard.
When Python reaches an input() statement, the program
pauses and waits for the user to enter a value. The user normally
presses Enter after entering the requested information.
input(prompt)
The optional prompt is the message displayed to the user
before Python waits for input.
name = input("Enter your name: ")
print("Hello", name)
Enter your name: Alice
Hello Alice
In this example:
Alice.input() is stored in
name.print() function displays a greeting.
One of the most important things to understand about Python's
input() function is that it returns the user's input as a
string.
Even if the user enters a number, input() initially returns
that value as text.
age = input("Enter your age: ")
print(age)
print(type(age))
Enter your age: 20
20
<class 'str'>
Although the user entered 20, Python stores the result as a
string. This is important when you need to perform mathematical
calculations with user input.
If you need an integer from the user, convert the result of
input() using the int() function.
age = int(input("Enter your age: "))
print("Your age is", age)
Enter your age: 20
Your age is 20
The input() function first receives the value as text, and
int() converts that text into an integer.
Use the float() function when the user needs to enter a
decimal number.
price = float(input("Enter the price: "))
print("Price:", price)
Enter the price: 25.50
Price: 25.5
The float() function converts the input into a floating
point number.
The most common conversion functions used with
input() are int() and float().
| Function | Purpose | Example |
|---|---|---|
input() |
Receives user input as a string | input("Enter name: ") |
int() |
Converts a value to an integer | int("25") |
float() |
Converts a value to a floating-point number | float("25.5") |
str() |
Converts a value to a string | str(25) |
A common beginner Python program asks the user for two numbers and calculates their sum.
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
total = num1 + num2
print("Sum =", total)
Enter first number: 5
Enter second number: 7
Sum = 12
The two values are converted to integers before the addition takes
place. Without the int() conversion, Python would treat
the values entered by the user as strings.
Because input() returns a string, using two input values
with the + operator can produce string concatenation rather
than numerical addition.
a = input("Enter first value: ")
b = input("Enter second value: ")
print(a + b)
Enter first value: 5
Enter second value: 7
57
The result is 57 rather than 12 because
a and b are strings. Python joins the two
strings together.
To perform numerical addition, convert the values to integers:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(a + b)
Enter first number: 5
Enter second number: 7
12
The print() function can display text and variables
together by separating them with commas.
name = "Alice"
score = 90
print("Student:", name)
print("Score:", score)
Student: Alice
Score: 90
f-strings, also called formatted string literals, are a convenient way to include variables and expressions inside strings. They were introduced in Python 3.6 and are widely used for formatting output.
Place the letter f before the opening quotation mark and
put variables or expressions inside curly braces.
name = "Alice"
age = 25
print(f"My name is {name} and I am {age} years old.")
My name is Alice and I am 25 years old.
An f-string can also contain Python expressions.
price = 50
quantity = 3
print(f"Total: {price * quantity}")
Total: 150
Python evaluates the expression inside the curly braces before displaying the result.
The print() function has a sep parameter that
controls the separator placed between multiple values.
print("2026", "08", "09", sep="-")
2026-08-09
By default, print() uses a space as the separator.
The sep parameter allows you to change it.
By default, print() moves to a new line after displaying
its output. The end parameter can be used to change this
behavior.
print("Hello", end=" ")
print("Python")
Hello Python
Here, end=" " tells Python to place a space after
Hello instead of moving immediately to a new line.
The print() function normally displays each call on a
separate line.
print("Line one")
print("Line two")
print("Line three")
Line one
Line two
Line three
The newline character \n can be used to create a new line
inside a string.
print("Python\nProgramming")
Python
Programming
The \n escape sequence tells Python to move the output to
the next line.
Python strings can contain escape sequences that control how text is
displayed. For example, \n creates a new line and
\t creates a tab space.
print("Name:\tAlice")
print("Age:\t25")
Name: Alice
Age: 25
A Python program can ask the user for several pieces of information.
Each call to input() waits for the user to enter a value.
name = input("Enter your name: ")
city = input("Enter your city: ")
print("Name:", name)
print("City:", city)
Enter your name: Alice
Enter your city: London
Name: Alice
City: London
The optional prompt passed to input() makes a program
easier to understand because it tells the user what information is
required.
For example:
name = input("Please enter your name: ")
print("Welcome,", name)
Please enter your name: Alice
Welcome, Alice
A clear prompt is an important part of designing a user-friendly Python program.
The following example combines input, type conversion, calculation and output to create a simple calculator.
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))
result = num1 + num2
print(f"Result = {result}")
Enter first number: 10.5
Enter second number: 4.5
Result = 15.0
If the user enters text that cannot be converted into the requested
numeric type, Python raises an exception. For example,
int() cannot convert the text hello into an
integer.
try:
age = int(input("Enter your age: "))
print("Age:", age)
except ValueError:
print("Please enter a valid number.")
Enter your age: hello
Please enter a valid number.
Using try and except allows a program to handle
invalid input more gracefully instead of terminating with an error.
The input() function returns a string, so values such as
True and False are not automatically converted
to Boolean values.
For example, entering True produces the string
"True", not the Boolean value True.
When a program needs Boolean input, it is generally better to explicitly
interpret the user's response rather than directly using
bool(input()).
The print() function provides several useful parameters
for controlling output.
| Parameter | Purpose | Example |
|---|---|---|
sep |
Separates multiple values | print("A", "B", sep="-") |
end |
Controls what is printed after the output | print("Hello", end=" ") |
file |
Specifies where output is written | print("Hello", file=file) |
flush |
Controls whether the output stream is flushed | print("Hello", flush=True) |
For beginners, sep and end are the most commonly
used parameters. The file parameter becomes useful when
working with files and streams.
The following program demonstrates several concepts together. It asks the user for a name, age and test score and then displays the information.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
score = float(input("Enter your score: "))
print()
print("Student Information")
print("-------------------")
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Score: {score}")
Enter your name: Alice
Enter your age: 20
Enter your score: 87.5
Student Information
-------------------
Name: Alice
Age: 20
Score: 87.5
The input() and print() functions have
different purposes.
| Function | Purpose | Direction |
|---|---|---|
input() |
Receives information from the user | User → Program |
print() |
Displays information to the user | Program → User |
Beginners often encounter a few common problems when using
input() and print().
input() returns a string.
int() when the user may enter decimal values.
print().
Consider this example:
a = input("Enter first number: ")
b = input("Enter second number: ")
print(a + b)
If the user enters 5 and 7, the output is
57 because both values are strings.
The solution is to convert the input:
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))
print(a + b)
Enter first number: 5
Enter second number: 7
12
input() returns a string.
int() or float()
when necessary.
sep and end when you need more control over
print().
try and except when appropriate to handle
invalid input safely.
Input is information provided to a Python program, commonly through the
keyboard using input(). Output is information displayed by
the program, commonly using print().
The input() function pauses the program and waits for the
user to enter information. It returns the entered value as a string.
The print() function displays text, variables, values and
the results of expressions on the screen.
Yes. The input() function returns the user's input as a
string. If you need an integer or decimal number, you can convert the
result using int() or float().
Use int() around the input() function:
age = int(input("Enter your age: "))
Use float() around input():
price = float(input("Enter the price: "))
Pass the variable to the print() function:
name = "Alice"
print(name)
You can pass multiple values to print() or use an f-string.
For example:
name = "Alice"
print("Hello", name)
One of the easiest ways to format output is to use an f-string:
name = "Alice"
age = 25
print(f"{name} is {age} years old.")
Python input and output allow programs to communicate
with users. The input() function receives information from
the user, while the print() function displays information
and results.
A particularly important rule for beginners is that
input() returns a string. When numerical calculations are
required, the input should normally be converted using
int() or float().
Python also provides several ways to control and format output,
including multiple arguments to print(), f-strings,
sep, end and escape sequences such as
\n.
Understanding Python input and output provides an important foundation for building interactive programs, calculators, games, data-processing scripts and larger Python applications.
Continue learning Python with these related tutorials:
Struggling with Python syntax, assignments or debugging errors? Learning alone can be frustrating.
Get 1-on-1 Python tutoring and learn faster with clear explanations, real examples and live help.
Home Tutorial Online Live Trainings Contact Us © 2026 All rights reserved by www.PythonTutor.net