
With the help of print
function in Python, we can display text or anything in the output. Using print
is very simple and practical; But at the same time, it has various complications and tricks. In this post, we will check how to work with Python's print
function and learn its practical tricks.
The print
function is one of the most used and useful functions in Python. It's used to see the result of work, give an output to the user or even debug.
This function is one of the built-in functions and we don't need to import a library or special tool to use it. We write it as print()
and give it the value we want to print in the output. For example:
print("I'm Albro's Hive Blog!")

The output of the above code will print the text in the application console.
The features and how to use the print
function in Python version 2 and 3 are similar. Their only main difference is in the way the function is called. In version 2, write the function without parentheses (as print
), and in version 3, we must use parentheses (print()
) to define the input arguments.
Print function in Python
If you have written codes in the Python Shell environment, the output will be printed immediately after the execution of each line.
As you can see in the image below, after writing each line, the result is displayed in the next line.

Now let's create a Python file called run.py
and put these codes in it.
"Hello World!"
7 + 22
"I'm Albro's Hive Blog!"
If we run this file, we will see nothing in the output!
In order for the output of each of the above code lines to be displayed in the output, we must use the print()
statement for each of them.
print("Hello World!")
print(7 + 22)
print("I'm Albro's Hive Blog!")

How to print with print in Python?
When an object or variable is passed to Python's print
function, Python first tries to convert it to a string and then displays it on the output.
In general, the input(s) to the print
function must be a Python string. If they're not strings, they're first converted to strings and then printed.
Let's continue with an example. The status of text strings that is clear! Suppose we want to print a mathematical calculation on the output. We write the following code for this:
print( (147 / 28) + 6)

If we run this code, we will not get the desired result! Running this line of code will give us the number 11.25 in the console. This number is written as the result of division and addition.
What is the reason for this?
The input value of the function is not a string. So Python tries to convert it to a string. In the process of converting a calculation formula into a string, its value is calculated as much as possible and the final result is converted into a string.
To solve this problem, we need to put the calculation formula inside the quotation marks ("
). In this way, we have given the print
function a string that should be printed directly.
Print Variable in Python
To print the value of the variable in the output, just put it in the print()
function.
Suppose we have two variables x
and s
. The first one is of numeric type and the other is of string type.
x = 27
s = "Albro"
To print these two variables in Python, we do the following.
print(x)
print(s)
The print
function in Python can take an unlimited number of inputs. If all these inputs are of type string or something that can be converted to a text string, it's like we wrote a print()
for each of the inputs separately.
As a result, instead of two lines of code above, both variables can be printed in the output using one statement.
print(x ,s)

Python's print
function can take infinite inputs and print them all one by one on the output.
Print different data types with print
In the example of the mathematical formula, we saw that in the print
function, the input is first converted into a string and then printed.
Most data types in Python have a string conversion function. This function is defined by default on all objects.
Suppose we have a Python list in which there are four names.
If we pass the list variable directly to the function, the output will be similar to the fourth line of the following code:
names = ["@lemouth", "@rocky1", "@howo", "@khaleelkazi"]
print(names)
# Result:
# ['@lemouth', '@rocky1', '@howo', '@khaleelkazi']

We said that Python tries to convert a non-string value to a string. This is done using the str()
function or the __str__
method.
By giving any variable or value to the str()
function, we will have its string value as output.
In object-oriented Python programming, by defining the __str__
method in the object, you can specify how the object is displayed in the print()
or str()
function. If this method is not defined, the id value of the object will be returned by default.
Print tricks in Python
Sometimes it is necessary to convert several values or variables into strings and display them next to each other in the output.
For this, we usually convert the variables into strings using the str()
function and then connect them to each other using the +
operator.
Note that if you try to concatenate the values without converting them to strings, you will get an error.
For example, in the following code we try to connect a numeric variable to two other constant strings.
age = 27
name = "Albro"
print(name + " have " + age + " years old!")

The corrected code above will be as follows:
age = 27
name = "Albro"
print(name + " have " + str(age) + " years old!")
Another solution is to convert the current input to several different inputs. For this, we need to give the age
variable, which is not a string, as a separate input to the print
function in Python.
age = 27
name = "Albro"
print(name + " have" ,age ,"years old!")
Python's print function arguments
For the print()
function in Python, there are four other arguments that are optional. In other words, this function is defined as follows:
print( object(s), sep=separator, end=end, file=file, flush=flush )
The four optional entries defined in the above line are:
- Separator (sep): If the function is given multiple strings to print, it will separate them using the value of this variable.
- End character (end): The character that is placed at the end of the printed line. Defaults to
\n
(newline). - File: Instead of printing the text in the output, it saves it in a specified file.
- flush: a value of
0
or1
that specifies whether the output isflushed
orbuffered
. (The default is the second type)
The most we use of these arguments are their first two.
Working with the sep
separator in Python print
Suppose we want to write the names of three people in the output and separate them from each other with the character ,
.
One solution to do this is to define the sep
argument in Python print
function. Note the following code:
print("@Albro", "@xeldal", "@minnowbooster", sep=",")
# Result:
# @Albro,@xeldal,@minnowbooster
The sep
input value can be a single character or a long string! For example, in the code below, we have used the ->
sign to display a hypothetical address.
print("@ecency.", "Hive Blockchain.", "No. 1", sep=" -> ")
# Result:
# @ecency. -> Hive Blockchain. -> No. 1
The end character of the text with the end argument in Python
When we use the print()
statement, by default, after printing the result, it goes to the beginning of the next line.
For example, in the following code, we have used the function three times. As you can see, after each run, it has gone to a new line:
print("@leo.voter")
print("@steemstem")
print("@enki")
# Result:
# @leo.voter
# @steemstem
# @enki
Python inserts a newline character (/n
) at the end of text printed to the console.
We can change this character by defining the end argument. The defining character can be anything! In the code below, a space is used.
print("@leo.voter", end=" ")
print("@steemstem", end=" ")
print("@enki", end=" ")
# Result:
# @leo.voter @steemstem @enki
Pay attention that there is a blank space at the end of the above result. This extra space is the end character corresponding to the print
's end
statement in the third line.
Conclusion
In this post, we learned about the print
function in Python. This function is defined as print()
in the third version and as print
in the second version of Python.
In the simplest case, by giving an input, it can be displayed in the program's console output. We also learned how the values defined as input to this function are converted into strings.
Meanwhile, we learned about the str()
side function and the __str__
method, which convert the object or value into a string. We have seen that the print statement in Python has four other arbitrary input arguments.
The sep
argument to define the separator value when printing multiple strings and the end
argument to define the end character of string printing are the two most used arguments of this function.