
Table of Contents
- Introduction
- Calling the print() function
- Printing a message
- Printing multiple messages
- Changing the separator
- Useful examples of printing
Introduction
The print() function is one of the basic built-in functions in Python.
In this article, we take a look at what the print() function exactly does and how can it be used in Python.
You are about to discover how to output text and numbers, change the separator, and loads of other amazing stuff.
By the end of this article, you’ll have a solid foundation about the print() function with useful real-life examples to enhance your learning experience.
Without squandering your valuable time, let’s start off.
The purpose of print() function
First of all, let’s discuss the meaning of print. In programming, to print means to output a message by writing it to the screen.
The print() function is a built-in function in Python. It is used to output the specified messages by writing them to the screen.
Calling the print() function
The print() function can be called at its most basic level like this:
>>> print()
By calling the print() function, we get a blank line printed to the screen.
In other words, we have printed an empty message.
Printing a message
We can print a message using the print() function like this:
>>> print(message)
where message is the object that we want to print.
Note: We’ll be learning about objects in a later article. For now you can think of an object as a way to store data.
There are three basic object types that we’ll be taking a look at in this article.
- String (collection of characters surrounded by quotes)
- Number
- Integer (whole number)
- Float (number with a decimal point)
- Boolean (true or false)
The message can be any object but will always be converted into a string before being printed.
Printing a string
We are going to start with the “Hello, World!” program which just prints Hello, World! to the screen.
Let’s create a new file named hello_world.py for this program.
hello_world.py
——————————————
print("Hello, World!")
The output is the same that you would expect:
Hello, World!
Note: the surrounding quotes are not displayed.
When a string is printed, the surrounding quotes do not get printed.
Remember that we can use double quotes instead of single quotes.
>>> print("Hello World")
Hello World
Printing a number
We can also be able to print a number using the print() function:
>>> print(21)
21
>>> print(2.5)
2.5
The number can also be a result of a calculation:
>>> print(5 + 5)
10
>>> print(10 - 5/2)
7.5
>>> print(3**2 // 2)
4
Printing multiple messages
The syntax for printing multiple messages the screen is:
>>> print(message1, message2, …)
By default, the messages get printed on the same line and are separated by a space.
Let’s have a look few examples:
>>> print(10, 20, 30, 40)
10 20 30 40
>>> print('This is', "John's", '"book"')
This is John's "book"
>>> print("The answer is", 5**2)
The answer is 25
Passing an empty string is equivalent to passing no arguments to the print() function.
>>> print("")
>>> print()
As you can see only a blank line is printed in both cases.
Changing the separator
In the previous part you saw that the message were separated by spaces. The separator can be changed to whatever you want.
The syntax for changing the separator is:
>>> print(message1, message2, …, sep=separator)
The separator must be a string containing the characters to be put between the messages. It can also be None which means that the default value (space) will be used.
Let’s look at an example from the previous part:
>>> print(10, 20, 30, 40, sep="+")
10+20+30+40
Note: the messages got separated by a plus sign (+) instead of a space.
Useful examples of printing
We can show calculations by separating the expressions with an equals sign ( = ).
>>> print('5+9', '5+5+4', 5+9, sep=' = ')
5+9 = 5+5+4 = 14
