Python Input & Output
Learn how Python communicates with users using
print() and input(),
how to format output, collect information, and
convert user input into useful data.
What You'll Learn
- Understand input and output in Python.
-
Use the
print()function. -
Control output using
sepandend. -
Collect information using
input(). - Convert user input into numbers.
- Build interactive Python programs.
- Format output using f-strings.
What Is Input & Output?
Input and output are fundamental parts of almost every useful program.
Input means receiving information from a user or another source.
Output means displaying or producing information for the user or another system.
The program asks for information, receives it, processes it, and then provides a result.
name = input("What is your name? ")
print("Hello", name)
What is your name? Ali Hello Ali
The print() Function
The print() function is used to display
information on the screen.
Basic Example
print("Hello, Python!")
Hello, Python!
You can print text, numbers, variables, calculations, and many other Python objects.
Printing Different Values
The print() function can display different
types of values.
print("Python")
print(100)
print(3.14)
print(True)
name = "Ahmed"
print(name)
Python 100 3.14 True Ahmed
Printing an Expression
print(10 + 20)
print(50 * 2)
30 100
Printing Multiple Values
You can pass multiple values to print().
Python separates them with a space by default.
name = "Ali"
age = 25
print("Name:", name, "Age:", age)
Name: Ali Age: 25
This is useful when you want to combine several pieces of information in one output statement.
The sep Parameter
The sep parameter controls what appears
between multiple values passed to print().
Default Separator
print("Python", "Java", "C++")
Python Java C++
Custom Separator
print("2026", "09", "12", sep="-")
2026-09-12
You can use almost any string as the separator.
print("A", "B", "C", sep=" | ")
A | B | C
The end Parameter
By default, print() moves to a new line
after displaying its output.
print("Hello")
print("World")
Hello World
The end parameter allows you to change
what happens at the end of the output.
print("Hello", end=" ")
print("World")
Hello World
Using end with a Symbol
print("Loading", end="...")
print("Done")
Loading...Done
Formatting Output
Good output should be easy for the user to understand. Python gives you several ways to combine text and values.
Using Commas
name = "Sara"
score = 95
print("Student:", name, "Score:", score)
Student: Sara Score: 95
Using String Concatenation
name = "Sara"
print("Hello " + name)
Hello Sara
You cannot directly concatenate a string and an
integer using +. Convert the number
first or use an f-string.
The input() Function
The input() function allows a Python program
to receive information from the user.
name = input("Enter your name: ")
print(name)
Enter your name: Ahmed Ahmed
When Python reaches input(), it pauses and
waits for the user to type something and press Enter.
The value returned by input() is
always a string.
Storing User Input
Usually, we store user input inside a variable so we can use it later.
name = input("What is your name? ")
city = input("Where do you live? ")
print("Name:", name)
print("City:", city)
What is your name? Ali Where do you live? Multan Name: Ali City: Multan
Once stored, the values can be used multiple times.
Converting User Input
Because input() returns a string, numeric
input usually needs to be converted before calculations.
Integer Input
age = int(input("Enter your age: "))
print(age)
Floating-Point Input
price = float(input("Enter price: "))
print(price)
Boolean Input
Be careful when converting strings directly with
bool().
print(bool("False"))
True
The string "False" is not an empty string,
so it evaluates to True.
Working with Multiple Inputs
You can collect several pieces of information from a user
using separate input() calls.
name = input("Name: ")
age = int(input("Age: "))
city = input("City: ")
print("Name:", name)
print("Age:", age)
print("City:", city)
Name: Hamza Age: 22 City: Lahore
Each input can be converted independently depending on the type of information expected.
Input and Calculations
Once numeric input has been converted, it can be used in mathematical expressions.
Adding Two Numbers
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))
total = num1 + num2
print("Total:", total)
Enter first number: 15 Enter second number: 25 Total: 40
Calculating a Rectangle Area
length = float(input("Enter length: "))
width = float(input("Enter width: "))
area = length * width
print("Area:", area)
Enter length: 10 Enter width: 5 Area: 50.0
Input with f-Strings
F-strings provide a clean and readable way to insert variables into strings.
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print(f"Hello {name}, you are {age} years old.")
Enter your name: Sara Enter your age: 24 Hello Sara, you are 24 years old.
F-strings are especially useful when creating readable messages containing multiple variables.
For modern Python code, f-strings are usually one of the clearest ways to format output.
Escape Characters
Escape characters allow you to represent special formatting inside strings.
New Line: \n
print("Hello\nWorld")
Hello World
Tab: \t
print("Name:\tAli")
Name: Ali
Quotation Marks
print("He said \"Hello\"")
He said "Hello"
Common Input & Output Mistakes
Mistake 1: Forgetting Input Is a String
age = input("Age: ")
print(age + 5)
This produces a TypeError because
age is a string.
Correct:
age = int(input("Age: "))
print(age + 5)
Mistake 2: Concatenating a Number Directly
age = 25
print("Age: " + age)
The string and integer cannot be concatenated directly.
Use an f-string:
age = 25
print(f"Age: {age}")
Mistake 3: Invalid Numeric Input
age = int(input("Age: "))
If the user enters twenty instead of
20, Python raises a ValueError.
In upcoming lessons, you will learn how to safely
handle invalid user input using
try and except.
Practice Exercises
Exercise 1
Write a program that asks the user for their name and prints a welcome message.
Exercise 2
Ask the user for two integers and print their sum.
Exercise 3
Ask the user for their age and print their age five years from now.
Exercise 4
Ask for the length and width of a rectangle and calculate its area.
Exercise 5
Predict the output:
print("Python", "is", "awesome", sep="-")
Exercise 6
Create a program that asks for a student's name, age, and marks, then displays all three values using an f-string.
Mini Challenge
Student Result Calculator
Build a small interactive program that asks the user for:
- Student name
- English marks
- Mathematics marks
- Computer Science marks
Calculate the total and average marks, then display the result in a clean format.
For example:
Student: Ali
Total Marks: 255
Average: 85.0
Use int(input()) for the marks
and an f-string to display the final result.
Quick Knowledge Check
Test your understanding of Python input and output.
1. Which function is used to display output?
2. Which function is used to receive user input?
3. What data type does input() return?
4. Which parameter changes the separator between printed values?
5. What does print("A", "B", sep="-") produce?
6. Which conversion is appropriate for whole-number input?
7. Which parameter controls what print() adds after its output?
Lesson Summary
Input and output allow Python programs to communicate with users and make programs interactive.
print() displays information.
input() receives information from
the user.
input() always returns a string.
int() or float()
when numeric input is required.
sep controls the separator between
printed values.
end controls what appears after
printed output.
ValueError.
A good Python program doesn't just perform
calculations — it communicates clearly with
the person using it. Mastering
input() and print()
is an important step toward building interactive
programs.
Finished Lesson 09?
Mark this lesson as complete and continue to Python's arithmetic operators.