The print
function in Python is a built-in function that is used to display output on the console or terminal window. It is one of the most commonly used functions in Python, and it is an essential tool for debugging and testing code.
Syntax:
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
The print
function takes one or more objects as input and outputs them to the console. The sep
argument is used to specify the separator between multiple objects, and the end
argument is used to specify what character or string is printed at the end of the output. The default value of sep
is a space and the default value of end
is a newline character.
For example:
print("Hello, World!")
This code will output the string "Hello, World!"
to the console.
print("Hello", "World", "!")
This code will output the string "Hello World !"
to the console, with a space between each of the objects.
print("Hello", "World", "!", sep="-")
This code will output the string "Hello-World-!"
to the console, with a dash between each of the objects.
print("Hello", "World", "!", end="***")
This code will output the string "Hello World !***"
to the console, with "***"
at the end of the output.
The file
argument is used to specify the file object to which the output is written. By default, the output is written to the standard output stream sys.stdout
. The flush
argument is used to control whether the output is flushed immediately or buffered until it is explicitly flushed.
The print
function is a very flexible tool that can be used for a wide variety of purposes. It is often used to display intermediate results or to debug code, but it can also be used to output text to the console, display results to the user, or even write to a file.
In conclusion, the print
function is a powerful and versatile tool in Python that is used to display output to the console. Whether you are writing a simple script or a complex application, it is an essential tool that you should be familiar with.