PyLearn Learn Python Step by Step
PyLearn / Python Foundations / Input & Output
Lesson 09 · Foundations Beginner

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.

30–40 minutes Python Foundations
0%

What You'll Learn

  • Understand input and output in Python.
  • Use the print() function.
  • Control output using sep and end.
  • Collect information using input().
  • Convert user input into numbers.
  • Build interactive Python programs.
  • Format output using f-strings.
01

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.

Think of a program as a conversation

The program asks for information, receives it, processes it, and then provides a result.

name = input("What is your name? ")

print("Hello", name)
Example Output
What is your name? Ali
Hello Ali
02

The print() Function

The print() function is used to display information on the screen.

Basic Example

print("Hello, Python!")
Output
Hello, Python!

You can print text, numbers, variables, calculations, and many other Python objects.

03

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)
Output
Python
100
3.14
True
Ahmed

Printing an Expression

print(10 + 20)
print(50 * 2)
Output
30
100
04

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)
Output
Name: Ali Age: 25

This is useful when you want to combine several pieces of information in one output statement.

05

The sep Parameter

The sep parameter controls what appears between multiple values passed to print().

Default Separator

print("Python", "Java", "C++")
Output
Python Java C++

Custom Separator

print("2026", "09", "12", sep="-")
Output
2026-09-12

You can use almost any string as the separator.

print("A", "B", "C", sep=" | ")
Output
A | B | C
06

The end Parameter

By default, print() moves to a new line after displaying its output.

print("Hello")
print("World")
Output
Hello
World

The end parameter allows you to change what happens at the end of the output.

print("Hello", end=" ")
print("World")
Output
Hello World

Using end with a Symbol

print("Loading", end="...")
print("Done")
Output
Loading...Done
07

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)
Output
Student: Sara Score: 95

Using String Concatenation

name = "Sara"

print("Hello " + name)
Output
Hello Sara
Watch the data types

You cannot directly concatenate a string and an integer using +. Convert the number first or use an f-string.

08

The input() Function

The input() function allows a Python program to receive information from the user.

name = input("Enter your name: ")

print(name)
Example Output
Enter your name: Ahmed
Ahmed

When Python reaches input(), it pauses and waits for the user to type something and press Enter.

Important rule

The value returned by input() is always a string.

09

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)
Example Output
What is your name? Ali
Where do you live? Multan
Name: Ali
City: Multan

Once stored, the values can be used multiple times.

10

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"))
Output
True

The string "False" is not an empty string, so it evaluates to True.

11

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)
Example Output
Name: Hamza
Age: 22
City: Lahore

Each input can be converted independently depending on the type of information expected.

12

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)
Example Output
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)
Example Output
Enter length: 10
Enter width: 5
Area: 50.0
13

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

Recommended approach

For modern Python code, f-strings are usually one of the clearest ways to format output.

14

Escape Characters

Escape characters allow you to represent special formatting inside strings.

New Line: \n

print("Hello\nWorld")
Output
Hello
World

Tab: \t

print("Name:\tAli")
Output
Name:   Ali

Quotation Marks

print("He said \"Hello\"")
Output
He said "Hello"
15

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.

Error handling comes later

In upcoming lessons, you will learn how to safely handle invalid user input using try and except.

16

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.

17

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
Hint

Use int(input()) for the marks and an f-string to display the final result.

18

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?

19

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.
Use int() or float() when numeric input is required.
sep controls the separator between printed values.
end controls what appears after printed output.
F-strings provide a clean way to format output.
Invalid numeric input can cause ValueError.
Key Takeaway

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.