Similar to other pro­gram­ming languages, you can use the print function in Python to publish strings and results on a screen. Ad­di­tion­ally, you can also set different para­met­ers to further define Python print.

What is Python print used for?

The print function is one of the first things that appears in our Python tutorial, and for good reason. For many ap­plic­a­tions, it is essential to be able to publish strings or in­ter­me­di­ate results on a screen. While the classic ‘Hello World’ program is a popular example, many other ap­plic­a­tions also use the Python print function.

Although it might not be best practice, the function is often used to debug simple programs. If you write a cor­res­pond­ing print in­voc­a­tion for a Python for loop or a Python while loop, you can find out the number of loops. You can also use Python print to determine if a Python if-else statement has been carried out. A well-known example for using the Python print function is the ‘Hello World’ program:

print("Hello World!")
Python

You can use just one parameter, the Python string ‘Hello World!’ to invoke the print function. It will then be published on the screen by the function in­voc­a­tion.

Tip

Using Python for a web-based project? With Deploy Now from IONOS, you can deploy your project at any time using GitHub.

What is the syntax for Python print?

The syntax for Python print is straight­for­ward:

print(object, sep=seperator, end=end, file, flushed)
Python

Although the function can use up to five para­met­ers, you only need to enter the first one. In the first parameter you need to specify exactly what you want published. You can use any Python object. You can also use multiple objects at the same time, just make sure to separate them using a comma.

print("hello", "world")
Python

What other para­met­ers are there for Python print?

Apart from the object you want to publish, the print function has four other para­met­ers, which are optional. These are written behind the object, as shown above, and separated by a comma.

Parameter De­scrip­tion Example
sep=separator This parameter lets you specify a separator, which is then inserted between in­di­vidu­al objects. The default value is a space: ‘ ’. print(“Hello”, “World”, sep=“:::“) will give you the following string: Hello:::World
end=end With this parameter, you can specify what should come at the end of the string. The default value is \n (a line break). print(“Hello”, “World”, end=“:::“) will give you the following string: Hello World:::
file=filename This parameter is used to say where it should be published. The default value here is stdout (standard output). However, you can record every object that has a write method. With open(‘output.txt’, ‘w’) as outfile: print(“Hello World”, file=outfile) writes the string ‘Hello World’ to the text file output.txt.
Flush=true value The Boolean parameter indicates whether the output needs to be flushed or not. The default value is ‘False’. print(“Hello World”, flush=True) will ensure that the string is flushed, i.e. directly published.

Keep in mind that when invoking Python print, the last four para­met­ers are optional. You can use one, more than one or none of them.

Go to Main Menu