Arguments of Print() in Python

Python is a simple language. It has the best syntax very easy to grasp for beginners. Printing is a very simple task, and all it requires is an inbuilt function print().

In this beginners tutorial, the functionality, syntax, and features of the print() function will be discussed. Get your seatbelts on for a fun ride.

These days, almost everybody’s programming journey starts with:

print("Hello World")

It is simple to print in python, but there is some more useful functionality to the function. Let us discuss about it.

Printing is simple and important because it shows if the program is working correctly and it plays a major role in debugging an erroneous code. Whatever program we write, a simple one to a huge complex one, unless and until we print something onto the console or the screen, it stays in the backend.

The process goes on, computations happen, but the user won't be able to see the output.

First, the most frequently and generally used function for printing is python, the print() function. Let us learn about it in detail.

Syntax:                                                  

print(values, sep = '', end = '\n', file = file, flush = flush)

Parameters of the function:

values:

The value that we want to print. It can be of any data type, but the function will convert it into a string while printing.

sep :

short for separator. It is an optional parameter, and it is used to specify the separating character between the values we print. The default sep is a space - > sep = ' '.

end :

It is an optional parameter. It is used to specify the character to be printed at the end. Default value is a new line character – end = ‘\n’.

file:

Optional parameter. It specifies where the output object has to written to. The default value is sys.stdout.

flush:

Optional parameter. It is a Boolean value that shows if the function's output is True or false (buffered). The default value is False.

Returns:

The function returns the output or the string we want to print onto the console.

Generally, when we print, we won't pass these arguments. This tutorial is all about these optional parameters and their use.

1. Values:

The value we want to print can have a varying data type, but in the end, everything becomes a string. Two useful literals here are an empty character and a new line character.

print(): If we want to leave a blank line before printing something. It prints an empty line.

What if we want to print a part of a string in a new line but in a single print() statement? We can use a special character- the new line character - \n.

It jumps into a new line wherever we want in print() statement.

Example:

print("Hello \nWorld")

print("Hello")

print("")

print("World")

Output:

Hello

World

Hello

World

Understanding:

In the first print statement, we used the \n. From the very next character from the \n, World is printed to the next line, as you can observe in the output.

In the next three print statements, the first one prints Hello, the second one leaves a blank line, and the third one prints the next string in the next line.

2. sep

The function can accept any number of items. By default, Python separates these items with space and prints them as a string. We can control or change that.

Syntax:

print(“statement1”, ”statement2”,…, sep = “character”)

The output will be in the form of

statement1characterstatement2characterstatement3..

We will get the space as the default separator if we don't specify any sep value.

Example:

print("hello","What's","up?", sep = "-")

Output:

hello-What's-up?

Understanding:

We specified – as the separating character. Hence, the three separate strings "hello", "What's", and "up?” are separated by – in the output.

3. end

In Python, after the execution of a print statement, in the end, the cursor shifts to the next line in the console window. In the syntax of the print() statement, by default, the character for the end is given as "\n" – The next line character. We can change that.

Observe the difference:

  1. By default:
print("hello")

print("World")

Output:

hello

World

2. Using end = “character”:

print("hello", end = " ")

print("World")

Output:

hello World

Understanding:

In the first code, we did not give any end character. So, the default next line character is taken, and the next print statement is printed in the next line. In the next code, we specified the end character as space. Hence, the next print statement is printed after a space after executing the first print statement.

Example using both sep and end:

print("#python", end = "")

print("is", "life", sep = "")

Output:

#pythonislife

Understanding:

Hashtags shouldn't have spaces, remember? Python is a file broken down. To avoid space between python and is, we gave the end = "-> empty. To avoid space between is and life, we used the sep and gave the separator as an empty character.

3. file:

This parameter specifies the output receiver, determining where the print() function should print/ write in. By default, it is sys. stdout, which means standard output.

This parameter is introduced in the python 3 versions; we can't find it in the previous pythons.

You may wonder if we can print into a normal text file by giving the same file for the file parameter by using this. Sure we can.

Here is an example:

File1 = open('myfile.txt', 'w')

print('Tutorial', file = File1)

File1.close()

Output:

If the file myfile.txt exists, “Tutorial” is printed or written into the file.

Understanding:

We opened the file we wanted to print into. In the print statement, we gave a string "Tutorial". We specified the file name for the file parameter and then closed the file. As a result, we get the string in the file.

4. Flush

Generally, in python, buffers are arranged to hold the inputs' and outputs' data. Hence, the data is used in chunks. The flush parameter helps the user to decide if the content needs to be clustered into buffers or not. The default value is false. If we give the value as true, the program's output will be given as a sequence of characters one by one.

If you want to use this functionality in python 2 or previous versions, you can use:

sys.stdout.flush()

Remember to import sys..