How to Print Multiple Arguments in Python?


Python is one of the most widely used programming languages due to its powerful dynamic functionalities and ease of writing codes. It is great for beginners as well to start their programming journey. One very popular start code or beginner code is to understand and implement printing ‘Hello world’ in our console. In python, we use a print() statement to achieve the same.

print("Hello World!!!")

In this article, we will try to understand the print() module and learn how to print multiple arguments in Python. When we try to display anything in the console or window, there are often situations where we need to print different types of variables or multiple arguments to justify the output. An argument is basically a value passed within a function when called. These individual variables or data sets are assigned to the parameter defined in the function.

For example,

Example

def demo(name,post):
    print("I am "+ name+" and I am a "+post)
demo("RDM", "Technical Writer")

Output

I am RDM and I am a Technical Writer

In the above method calling it with a single argument or no arguments is an error, as the method has a fixed number of arguments. Likewise, there are methods we can define with a variable number of arguments. Going ahead, we will try to understand printing multiple arguments. Remember print function is a String output so our focus is to achieve strings inside print statements.

Method 1: print() using multiple arguments

Using a simple print() function we can pass the multiple arguments and print the same. In the code below we will also see how we can easily assign an initial value to the arguments.

Example

def funInitial(name,course="DSA"):
    print("Hello, "+name+". You have been enrolled in "+course)
funInitial("RDM")
funInitial("RDM","DEVOPS")

Output

Hello, RDM. You have been enrolled in DSA
Hello, RDM. You have been enrolled in DEVOPS

So, as we can easily understand from the above code, the function takes in two arguments, one of them is already assigned a default value. We may pass one or two arguments on our choice and it gives us the output accordingly. We have used a simple print statement to print both the arguments.

Method 2: Using the f-Strings

After Python 3.6 we have a powerful update on formatted string literals or f-Strings. f-Strings help us to concisely format strings, including any variable, expression and other data types. They allow us to embed arguments or data types in { } braces.

Let’s look into the code part.

Example

def funInitial(name,course):
    print(f"Hello, {name}. You have been enrolled in {course[0]}. The course timeline is for {course[1]} months.")
funInitial("RDM",("FSD","9"))

Output

Hello, RDM. You have been enrolled in FSD. The course timeline is for 9 months.

Above we can see a very concise but powerful implementation of formatted strings. The function takes variable arguments in the form of a tuple of strings and how easily we can use it inside f-Strings and print the entire statement as necessary.

Method 3: Using the .format() method

We have a .format() function available in print function which helps us print multiple arguments. The function replaces { } placeholders with the arguments in sequence. This gives us a proper output as desired. Let’s try it in our code.

Example

def funInitial(name,course):
    print("Hello, {}. You have been enrolled in {}. The course timeline is for {} months.".format(name,course[0],course[1]))
funInitial("RDM",("FSD","9"))

Output

Hello, RDM. You have been enrolled in FSD. The course timeline is for 9 months.

Method 4: String Concatenation

As we know python displays string literal through print statement. We can easily convert any non string argument to String type. This is called explicit type casting. The ‘+’ operator helps us concat strings.

Example

def funInitial(num,bool):
    print("We are trying to print a number "+ str(num) + " and also a boolean " + str(bool))
funInitial(7,True)

Output

We are trying to print a number 7 and also a boolean True

See how we have printed multiple arguments just by typecasting them to strings and then concatenating them using the ‘+’ operator. Though, sometimes it can be a hefty task for long statements but is widely used in most scenarios.

Method 5: Pass the arguments as a Dictionary

Another way to print multiple arguments can be by passing the arguments as a Dictionary in the print statement. The %(key)s operator helps us use the keys to represent the value in the dictionary. Let’s understand through the code.

Example

def funInitial(name, years):
    print("Hello, %(n)s, you have been enrolled in a %(y)s-year programme." % {'n': name, 'y': years})
funInitial("RDM", 2)

Output

Hello, RDM, you have been enrolled in a 2-year programme.

The %()s acts as a string reference to the dictionary input and the keys can be used inside the brackets to specify which value to insert. This way of implementation is useful in many scenarios especially with a large dataset.

Method 6: Using *args and **kwargs

The simplest way to pass a variable number of arguments into a function is to use *args. The passed argument is not stored inside any variable. **kwargs on the other hand accepts variable no. of arguments but each argument has a name assigned to the same. Seems confusing, let us understand through our code.

Example

def funInitial(*args):
    for each in args:
        print(each)
def funFinal(**kwargs):
    for key, value in kwargs.items():
        print(key, value)
funInitial("Hello", "Learner1")
funFinal(name="Learner1", age=24)

Output

Hello
Learner1
name Learner1
age 24

Conclusion

Through this article, we have tried to explore the print statement to display multiple arguments in python. There are several ways to print multiple arguments in python. We have discussed the best possible ways. Choosing the right method you can implement and enhance the readability and clarity of the text output in the console.

Updated on: 29-Aug-2023

783 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements