print() function in python

Print() function in Python displays the input. Input can exists of texts, numbers, symbols, or any other data type in Python.

The result is either shown on the screen or wherever the programmer has chosen the input to be shown.

basic syntax of the print() function.

print(“Hello, World! We’re learning Python”)

This is the basic syntax of writing a print function in Python.

Where;

print() is the name of the function. 

There is also a pair of parentheses. It is necessary to use these parentheses with every single Python function because the arguments go inside these parentheses.

Arguments: Arguments are the values that go inside the parentheses of a function. It is the input we give to the function, and the function processes that input and comes up with some result.

In our example above, “Hello, World! We’re learning Python” is our argument. 

“ ”  The apostrophes tell python that the text inside them is a string. They only come when you pass a string value into the print() function. In the case of floats or integers, we don’t use them.

(click here to know more about the different data types in Python).

Arguments in Python print() function

The values that go inside a function are called arguments.

Let’s understand this concept with an example of mathematics.

In mathematics, you must have studied the square function. 

That takes a value and squares it.

x2

When we pass an argument into the square function, the function processes it and turns it into the square.  

For example, if we put 2 in our square function above, we get.

(2)= 4

2 is the argument that is going inside the function. 

And the output or the result is 4.

The print() function behaves similarly to a mathematics function. 

We give arguments to the print() function. Python reads those arguments, processes them, and displays the results.

How many arguments can we pass into the print() function in python?

In python, you can give as many arguments to the print() function as you like. There is no limit. 

Look at the example below.

print(“Hello, World! We’re learning Python”)

In the above code, we have only a single string argument.

print(“Hello, World!”, “We’re learning”, “Python”)

Now we have three different arguments.

You can add any number of arguments in the print() function. The limit is your imagination and your harddrive’s space. 

We separate each argument with a comma. Therefore, the comma is also known as a separator.

We learned that there is no limitation to the number of function arguments. But…

What is the minimum number of arguments that a print() function would process?

The print() function would still process even if you give zero or no arguments to it at all.

If you don’t give any arguments to the print() function and leave the parentheses empty. The python will provide an empty line as an output.

Look at the example below:

Input

>> print(“Hi, I’m learning Python”)
>> print()
>> print(“Today”)

Output

>> Hi, I’m learning Python
>>
>> Today

The 2nd line in the output section is empty. It is because we gave no argument in the input section to the print() function.

Advance print() function arguments in Python ( sep= , end=, flush=)

So far, we have talked only about one parameter of the print() function.

Here is the complete syntax of print() function:

print(object(s), sep=separator, end=end, file=file, flush=flush)

let’s learn about them one by one.

object(s)

The only argument in the print() function that is not optional and hasn’t any default value is object(s).

Object(s) could be strings, floats, decimals, nones, or other data types.

The user has to give object(s) argument(s) whenever they write a print function.

Example:

>> print(1, 2)

Here 1,2 are the decimal object arguments.

Sep:

sep is a type of keyword argument. We use it when we wish to separate two arguments with something else other than space. 

If you don’t write it in your code, by default, it would give a space between the arguments.

To use the separator argument we write sep, is equal sign(=) and commas. Inside the commas, we tell python by which means we wish to separate the arguments. 

Like this.

Input 

>> print(1,2, sep=“//”)

Now Python would give output as.

>> 1//2

end:

end is also a keyword argument.

the end argument tells python what to do once the print() function is executed.

By default, end is set to “\n” (newline). That means once the print() function is executed go to the next line.

We can alter it by using end argument in our print() function. We can tell Python what to do once the print() function is executed.

Just like this.

Input

>> print(1,2,3, end=“ ”)
>> print(4,5,6) 

Output

>> 1 2 3 4 5 6

Note 4 5 6 are printed in the same line with 1 2 3. It is because we told Python not to go to the next line once the function is executed. Instead, just give a single space and execute the next line. 

 

file

file keyword argument tells python where to display the output of the print() function. By default the, it is set to sys.stdout which means the output would be shown on the screen where the code is written.

In some cases, we need to show the output somewhere else. In that case we would use this argument.

flush:

flush is the keyword argument that expresses a boolean value specifying if the output flushed (True) or buffered (False). 

Buffered output means that the computer spools the output somewhere in memory until a certain amount has accumulated. Afterward, it writes the entire block. This is more effective than using unbuffered output, which writes the output as soon as you request for it to be written.

By default, Python sets flush to False.

Remember, other than object(s) arguments, all other arguments are optional. If you don’t write them, Python will carry them out with their default values.

Scroll to Top