Python Tutoring Online
 Home            

Python Input and Output – Using input() and print()

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.

What Are Input and Output in Python?

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.

Python Output Using the print() Function

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:

Python
print("Hello Python")

Output


Hello Python

The text inside the quotation marks is displayed on the screen.

Printing Numbers in Python

The print() function can display numbers as well as text. Numbers do not need quotation marks.

Python
print(10)
print(25.5)
print(100 + 50)

Output


10
25.5
150

Python evaluates the expression 100 + 50 before displaying the result.

Printing Variables in Python

The print() function can display the value stored in a variable.

Python
name = "Alice"
age = 25

print(name)
print(age)

Output


Alice
25

The values stored in name and age are displayed when the program runs.

Printing Multiple Values in Python

Python allows you to pass multiple values to print(). By default, Python separates the values with a space.

Python
name = "Alice"
age = 25

print(name, age)

Output


Alice 25

You can also combine text and variables in the same print() statement.

Python
name = "Alice"
age = 25

print("Name:", name)
print("Age:", age)

Output


Name: Alice
Age: 25

Python Input Using the input() Function

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.

Basic Syntax of input()


input(prompt)

The optional prompt is the message displayed to the user before Python waits for input.

Example: Basic User Input

Python
name = input("Enter your name: ")

print("Hello", name)

Output Example


Enter your name: Alice
Hello Alice

In this example:

input() Returns a String

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.

Python
age = input("Enter your age: ")

print(age)
print(type(age))

Output Example


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.

Receiving Integer Input in Python

If you need an integer from the user, convert the result of input() using the int() function.

Python
age = int(input("Enter your age: "))

print("Your age is", age)

Output Example


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.

Receiving Decimal Input with float()

Use the float() function when the user needs to enter a decimal number.

Python
price = float(input("Enter the price: "))

print("Price:", price)

Output Example


Enter the price: 25.50
Price: 25.5

The float() function converts the input into a floating point number.

Converting Python User Input

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)

Example: Adding Two Numbers from User Input

A common beginner Python program asks the user for two numbers and calculates their sum.

Python
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

total = num1 + num2

print("Sum =", total)

Output Example


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.

What Happens If You Add Strings Instead of Numbers?

Because input() returns a string, using two input values with the + operator can produce string concatenation rather than numerical addition.

Python
a = input("Enter first value: ")
b = input("Enter second value: ")

print(a + b)

Output Example


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:

Python
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

print(a + b)

Output Example


Enter first number: 5
Enter second number: 7
12

Printing Text and Variables Together

The print() function can display text and variables together by separating them with commas.

Python
name = "Alice"
score = 90

print("Student:", name)
print("Score:", score)

Output


Student: Alice
Score: 90

Using f-Strings for Python Output

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.

Python
name = "Alice"
age = 25

print(f"My name is {name} and I am {age} years old.")

Output


My name is Alice and I am 25 years old.

Using Expressions Inside an f-String

An f-string can also contain Python expressions.

Python
price = 50
quantity = 3

print(f"Total: {price * quantity}")

Output


Total: 150

Python evaluates the expression inside the curly braces before displaying the result.

Using sep with print()

The print() function has a sep parameter that controls the separator placed between multiple values.

Python
print("2026", "08", "09", sep="-")

Output


2026-08-09

By default, print() uses a space as the separator. The sep parameter allows you to change it.

Using end with print()

By default, print() moves to a new line after displaying its output. The end parameter can be used to change this behavior.

Python
print("Hello", end=" ")
print("Python")

Output


Hello Python

Here, end=" " tells Python to place a space after Hello instead of moving immediately to a new line.

Printing Multiple Lines in Python

The print() function normally displays each call on a separate line.

Python
print("Line one")
print("Line two")
print("Line three")

Output


Line one
Line two
Line three

Using the Newline Character in Python Output

The newline character \n can be used to create a new line inside a string.

Python
print("Python\nProgramming")

Output


Python
Programming

The \n escape sequence tells Python to move the output to the next line.

Printing Special Characters

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.

Python
print("Name:\tAlice")
print("Age:\t25")

Output


Name:   Alice
Age:    25

Taking Multiple Inputs from the User

A Python program can ask the user for several pieces of information. Each call to input() waits for the user to enter a value.

Python
name = input("Enter your name: ")
city = input("Enter your city: ")

print("Name:", name)
print("City:", city)

Output Example


Enter your name: Alice
Enter your city: London
Name: Alice
City: London

Using input() with a Prompt

The optional prompt passed to input() makes a program easier to understand because it tells the user what information is required.

For example:

Python
name = input("Please enter your name: ")

print("Welcome,", name)

Output Example


Please enter your name: Alice
Welcome, Alice

A clear prompt is an important part of designing a user-friendly Python program.

Example: Python User Input for a Simple Calculator

The following example combines input, type conversion, calculation and output to create a simple calculator.

Python
num1 = float(input("Enter first number: "))
num2 = float(input("Enter second number: "))

result = num1 + num2

print(f"Result = {result}")

Output Example


Enter first number: 10.5
Enter second number: 4.5
Result = 15.0

Handling Invalid Numeric Input

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.

Python
try:
    age = int(input("Enter your age: "))
    print("Age:", age)
except ValueError:
    print("Please enter a valid number.")

Output Example


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.

Python Input and Output with Boolean Values

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()).

Python print() Function Parameters

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.

Python Input and Output Example Program

The following program demonstrates several concepts together. It asks the user for a name, age and test score and then displays the information.

Python
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}")

Output Example


Enter your name: Alice
Enter your age: 20
Enter your score: 87.5

Student Information
-------------------
Name: Alice
Age: 20
Score: 87.5

Difference Between input() and print()

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

Common Python Input and Output Mistakes

Beginners often encounter a few common problems when using input() and print().

Common Error: Adding Input Values Without Conversion

Consider this example:

Python
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:

Python
a = int(input("Enter first number: "))
b = int(input("Enter second number: "))

print(a + b)

Output Example


Enter first number: 5
Enter second number: 7
12

Python Input and Output Best Practices

Frequently Asked Questions About Python Input and Output

What is input and output in Python?

Input is information provided to a Python program, commonly through the keyboard using input(). Output is information displayed by the program, commonly using print().

What is the input() function in Python?

The input() function pauses the program and waits for the user to enter information. It returns the entered value as a string.

What is the print() function in Python?

The print() function displays text, variables, values and the results of expressions on the screen.

Does input() return a string in Python?

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().

How do I take an integer input in Python?

Use int() around the input() function:


age = int(input("Enter your age: "))

How do I take a decimal input in Python?

Use float() around input():


price = float(input("Enter the price: "))

How do I print a variable in Python?

Pass the variable to the print() function:


name = "Alice"
print(name)

How do I print text and variables together in Python?

You can pass multiple values to print() or use an f-string. For example:


name = "Alice"
print("Hello", name)

How do I format output in Python?

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.")

Summary of Python Input and Output

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.

Related Python Tutorials

Continue learning Python with these related tutorials:

Need Help Learning Python?

Having difficulty understanding Python input, output, variables or data types? An experienced Python tutor can help you learn Python fundamentals through clear explanations and practical coding examples.

One-to-one Python tutoring provides guided coding practice and personalized help with Python fundamentals, assignments, projects and programming problems.

Get Python Tutoring Help




Stuck Learning Python?

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.

    Get Python Tutoring

PythonTutor.net

Home    Tutorial     Online Live Trainings    Contact Us           © 2026 All rights reserved by www.PythonTutor.net