In Python, we often need to display information on the screen. Whether it's a simple message, a calculation result, or the value stored in a variable, we can use the print() function.
If input() helps us get information from the user, print() helps us show information to the user.
What is print()?
The print() function is used to display output on the screen.
The simplest example is:
print("Hello, World!")Output:
Hello, World!You can print any text by putting it inside quotes.
print("Welcome to Python")
print("Let's learn programming")Output:
Welcome to Python
Let's learn programmingPrinting Variables
You can also print the values stored in variables.
name = "John"
age = 20
print(name)
print(age)Output:
John
20This is something you'll use constantly while writing Python programs.
Printing Multiple Values
The print() function can display multiple values at once.
name = "John"
age = 20
print("Name:", name, "Age:", age)Output:
Name: John Age: 20Python automatically places a space between the values.
Printing Calculations
print() can also display the result of an expression.
print(10 + 5)
print(20 * 3)Output:
15
60You can also use variables:
price = 100
quantity = 3
print(price * quantity)Output:
300Using sep
By default, print() separates multiple values with a space.
print("John", 20, "India")Output:
John 20 IndiaYou can change the separator using sep:
print("John", 20, "India", sep=" | ")Output:
John | 20 | IndiaThis can be useful when you want to control how multiple values are displayed.
Using end
Normally, every print() statement moves to a new line:
print("Hello")
print("John")Output:
Hello
JohnYou can change this behavior using the end parameter:
print("Hello", end=" ")
print("John")Output:
Hello JohnFor example, you could also use:
print("Loading", end="...")Output:
Loading...Printing Formatted Output
When working with variables, f-strings are a clean way to create formatted output.
name = "John"
age = 20
print(f"My name is {name} and I am {age} years old.")Output:
My name is John and I am 20 years old.The f before the string allows us to place variables directly inside {}.
You'll use f-strings a lot when building Python programs.
Printing Different Data Types
print() can display different types of values:
name = "John"
age = 20
height = 5.8
is_student = True
print(name)
print(age)
print(height)
print(is_student)Output:
John
20
5.8
TruePrinting a Blank Line
Calling print() without any value creates a blank line:
print("Hello")
print()
print("Welcome")Output:
Hello
WelcomeA Small Example
Let's put everything together:
name = input("Enter your name: ")
age = int(input("Enter your age: "))
print()
print("----- Student Information -----")
print(f"Name: {name}")
print(f"Age: {age}")
print(f"Next year you will be {age + 1}.")If the user enters John and 20, the output will be:
----- Student Information -----
Name: John
Age: 20
Next year you will be 21.So, the print() function is your main tool for displaying output in Python. As you learn more Python, you'll use it everywhere—from simple programs to debugging larger applications.