How to use Python print

Similar to other programming languages, you can use the print function in Python to publish strings and results on a screen. Additionally, you can also set different parameters 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 applications, it is essential to be able to publish strings or intermediate results on a screen. While the classic “Hello World” program is a popular example, many other applications 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 corresponding print invocation 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 invocation.

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 straightforward:

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

Although the function can use up to five parameters, 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 parameters are there for Python print?

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

Parameter Description Example
sep=separator This parameter lets you specify a separator, which is then inserted between individual 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 parameters are optional. You can use one, more than one or none of them.

Was this article helpful?
We use cookies on our website to provide you with the best possible user experience. By continuing to use our website or services, you agree to their use. More Information.
Page top