Different ways to pass arguments in the print() function

In this blog, you will learn how to pass arguments in the print() functions using 3 different ways …

  • how to feed the print() function with more than one argument

  • how to pass the argument using positional way and

  • how to pass the argument using a keyword way.


1. The print() function — using multiple arguments


print("This is" , "my first" , "Python blog.")

If you look at the above one-line code than it contains three arguments and all of them are strings but there is only one print() function invocation.

Here more visibility commas are used to separate the arguments but it’s not necessary. If you put the comma inside the string than the arguments play a completely different role. According to Python’s syntax, the latter is intended to be shown in the console.

Run the code and see what happens. You have seen the text as output:

This is my first Python blog.

We conclude from the above example:

  • a print() function invoked with more than one argument outputs them all on one line;

  • the print() function puts a space between the outputted arguments on its initiative.


2. The print() function — using the positional arguments


print("This is" , "my first" , "Python blog.")
print("And Python is an easy language to learn.")

Passing the arguments in a positional way means the argument is dictated by its position, e.g., the second argument will be outputted after the first.

The output of the code is:

This is my first Python blog.
And Python is an easy language to learn.


3. The print() function — using the keyword arguments


print("This is" , "my first", end=" ")
print("Python blog.")

When you want to change the print() function behavior then Python offers another mechanism for the passing of arguments called keyword arguments. It means the argument is not taken from its location (position) but from the special word (keyword) used to identify them.


The print() function has two keyword arguments.


1] The first keyword argument: end.

In order to use it, it is necessary to know some rules:

  • a keyword argument consists of three elements: a keyword identifying the argument (end ); an equal sign (=); and a value assigned to that argument;

  • any keyword arguments have to be put after the last positional argument (this is very important)

In our example, we have made use of the end keyword argument and set it to a string containing one space.

After running the code you can see the following text as output:


This is my first Python blog.

2] The second keyword argument: sep.


print("This is" , "my first", sep=" - ")
print("Python blog.")

If you want to separate your outputted arguments with a comma or something else rather then spaces then use the keyword argument sep.

Look at the code in the editor, and run it.

The sep argument gives the following output:

This is - my first - Python blog.

Thank you.

35 views0 comments

Recent Posts

See All