print() function in Python

 The print() function in Python is used to output text or variables to the console. It is one of the most commonly used functions in Python and is used for debugging, logging, and displaying program output.

Here's the basic syntax of the print() function:

python
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)

Let's break down each part of the syntax:

  • *objects: This is a variable-length argument list. You can pass in any number of objects or variables to print, separated by commas.

  • sep: This is the separator between the objects you're printing. By default, it is a single space character, but you can specify any string you want.

  • end: This is the character that comes at the end of the printed text. By default, it is a newline character (\n), but you can specify any string you want.

  • file: This specifies the file to which the output is sent. By default, it is sys.stdout, which means the output is sent to the console.

  • flush: This specifies whether to flush the output buffer after printing. By default, it is set to False, which means the output buffer is not flushed.

Here are some examples of how to use the print() function:

python
# Output a string to the console print("Hello, world!") # Output a variable to the console x = 42 print(x) # Output multiple variables to the console y = 3.14 print(x, y) # Specify a custom separator and end character print(x, y, sep=" | ", end="!!!\n")

Output:

Hello, world! 42 42 3.14 42 | 3.14!!!

Post a Comment

Previous Post Next Post