AMZ DIGICOM

Digital Communication

AMZ DIGICOM

Digital Communication

Python print: how the function works in Python

PARTAGEZ

Python uses the print function to display strings and results on the screen, like many other programming languages. You can optionally specify various parameters that further refine the behavior of Python print.

Scopes of the Python print function

The print function is one of the first functions presented in a Python tutorial And it is not without reason: being able to present strings or intermediate results on the screen is indeed a essential possibility in many applications. In addition to the classic “Hello World” program, many application programs also use the Python print function.

While certainly not one of the best programming practices, another common use of the print function is for debugging simple programs. If you write a corresponding appeal from print In a for loop in Python or a while loop in Python you can determine the number of loop executions. Similarly, a simple call to the print function can be used to find out if an if-else statement in Python has been executed.

A well-known example of calling the print function in Python is the “Hello World” program:

print("Hello World!")

Python

The print function is called here with a single parameter, the Python string “Hello World!” “. It is now displayed on the screen by the function call.

If you’re using Python for your web project, it’s worth checking out IONOS’ Deploy Now. With this handy tool, you can deploy your project at any time via GitHub.

Python print function syntax

The syntax of the Python print function is very simple:

print(objet, sep=séparateur, end=fin, fichier, flushed)

Python

The function accepts up to five parameters, but only the first parameter is required. Here you specify precisely what should be output. You can pass any Python object. Additionally, passing multiple objects at the same time, separated by commas, is also allowed:

print("hello", "world")

Python

Overview of Python print parameters

In addition to the object to be output, the print function can accept up to four additional optional parameters. As shown in the syntax above, they are written after the subject and separated by a comma.

Setting Description Example
sep=separator You can specify a separator to separate each object. The default is “”. print(“Hello”, “World”, sep=“:::“) returns the following string: Hello:::World
end=end You can specify the character to appear at the end of the printed output. The default is /n (the newline control character). print(“Hello”, “World”, end=“:::“) returns the following string: Hello World:::
file=filename This parameter allows you to specify the location where printing should take place. By default, the stdout (standard output) value is stored here. However, you can populate any 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=logical value The boolean parameter is used to specify whether the output should be flushed or not. The default value is false (False). print(“Hello World”, flush=True) Ensures that the string is flushed, ie its output is direct.

Note that when calling Python print, there is no need to pass all the optional parameters. This allows you to define the parameters required for your application on a case-by-case basis.

Télécharger notre livre blanc

Comment construire une stratégie de marketing digital ?

Le guide indispensable pour promouvoir votre marque en ligne

En savoir plus

Souhaitez vous Booster votre Business?

écrivez-nous et restez en contact