Python built-in Method - print()

The print() function is a built-in Python method that is used to output values to the console or standard output device. It takes zero or more arguments and prints them to the console, separated by spaces.

The syntax for the print() function is as follows:

print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
So‮ru‬ce:www.theitroad.com
  • objects: Zero or more objects to be printed.
  • sep (optional): Separator to be used between the objects, default is ' '.
  • end (optional): String to be printed at the end, default is '\n'.
  • file (optional): Output stream to be used for printing, default is sys.stdout.
  • flush (optional): If True, the output buffer is flushed after printing, default is False.

Here are some example usages of the print() function:

>>> print("Hello, world!")
Hello, world!

In this example, we use the print() function to output the string "Hello, world!" to the console.

>>> x = 3
>>> y = 4
>>> print("The sum of", x, "and", y, "is", x + y)
The sum of 3 and 4 is 7

In this example, we use the print() function to output a formatted string that includes the values of x, y, and their sum.

>>> for i in range(5):
...     print(i, end=" ")
...
0 1 2 3 4

In this example, we use the print() function to output a sequence of numbers using a for loop. We set the end parameter to a space character to separate the numbers by a space instead of a newline character.

The print() function is a fundamental built-in method in Python for displaying output and debugging. It is often used in console applications and scripts, as well as in scientific and mathematical programming applications.