The Wayback Machine - https://web.archive.org/web/20240828030115/https://www.geeksforgeeks.org/how-to-print-multiple-arguments-in-python/
Open In App

How to Print Multiple Arguments in Python?

Last Updated : 10 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

An argument is a value that is passed within a function when it is called.They are independent items, or variables, that contain data or codes. During the time of call each argument is always assigned to the parameter in the function definition.

Example: Simple argument

Python
def GFG(name, num):
    print("Hello from ", name + ', ' + num)


GFG("geeks for geeks", "25")

Output:

Hello from  geeks for geeks, 25

Calling the above code with no arguments or just one argument generates an error.

Variable Function Arguments

As shown above, functions had a fixed number of arguments. In Python, there are other ways to define a function that can take the variable number of arguments.
Different forms are discussed below:

  • Python Default Arguments: Function arguments can have default values in Python. We provide a default value to an argument by using the assignment operator (=).

Example:

Python
def GFG(name, num="25"):
    print("Hello from", name + ', ' + num)


GFG("gfg")
GFG("gfg", "26")

Output:

Hello from gfg, 25

Hello from gfg, 26

  • Pass it as a tuple
     
Python
def GFG(name, num):
    print("hello from %s , %s" % (name, num))


GFG("gfg", "25")

Output:

hello from gfg , 25

  • Pass it as a dictionary
     
Python
def GFG(name, num):
    print("hello from %(n)s , %(s)s" % {'n': name, 's': num})


GFG("gfg", "25")

Output:

hello from gfg , 25

  • Using new-style string formatting with number
     
Python
def GFG(name, num):
    print("hello from {0} , {1}".format(name, num))


GFG("gfg", "25")

Output:

hello from gfg , 25

  • Using new-style string formatting with explicit names
     
Python
def GFG(name, num):
    print("hello from {n} , {r}".format(n=name, r=num))


GFG("gfg", "25")

Output:

hello from gfg , 25

  • Concatenate strings
     
Python
def GFG(name, num):
    print("hello from " + str(name) + " , " + str(num))


GFG("gfg", "25")

Output:

hello from gfg , 25

  •  Using the new f-string formatting in Python 3.6
     
Python
def GFG(name, num):
    print(f'hello from {name} , {num}')


GFG("gfg", "25")

Output:

hello from gfg , 25

  • Using *args 
Python
def GFG(*args):
    for info in args:
        print(info)


GFG(["Hello from", "geeks", 25], ["Hello", "gfg", 26])

Output:

[‘Hello from’, ‘geeks’, 25]

[‘Hello’, ‘gfg’, 26]

  • Using **kwargs 
Python
def GFG(**kwargs):
    for key, value in kwargs.items():
        print(key, value)


GFG(name="geeks", n="- 25")
GFG(name="best", n="- 26")

Output:

name geeks

n – 25

name best

n – 26

How to Print Multiple Arguments in Python? – FAQs

How to do multiple arguments in Python?

In Python, functions can take multiple arguments in various ways. Here’s a simple example using positional arguments:

def multiple_arguments(arg1, arg2, arg3):
print(arg1, arg2, arg3)

multiple_arguments(1, 2, 3)

How to print multiple statements in Python?

To print multiple statements in Python, simply use multiple print functions or combine the text with newline characters (\n):

print("First statement")
print("Second statement")

# Or in a single print function
print("First statement\nSecond statement")

How to print multiple elements in Python?

You can print multiple elements by passing them as separate arguments to the print() function, which will output them separated by spaces:

a, b, c = 1, 2, 3
print(a, b, c) # Outputs: 1 2 3

How to print 3 values in Python?

Printing three specific values follows the same method as printing multiple elements:

x, y, z = 'apple', 'banana', 'cherry'
print(x, y, z) # Outputs: apple banana cherry

How to multiply arguments in Python?

To multiply arguments passed to a function, you can gather them using *args and multiply them using a loop or a functional approach:

def multiply_arguments(*args):
result = 1
for number in args:
result *= number
return result

print(multiply_arguments(2, 3, 4)) # Outputs: 24


Previous Article
Next Article

Similar Reads

Python - pass multiple arguments to map function
The map() function is a built-in function in Python, which applies a given function to each item of iterable (like list, tuple etc.) and returns a list of results or map object. Syntax : map( function, iterable ) Parameters : function: The function which is going to execute for each iterableiterable: A sequence or collection of iterable objects whi
3 min read
Executing functions with multiple arguments at a terminal in Python
Commandline arguments are arguments provided by the user at runtime and gets executed by the functions or methods in the program. Python provides multiple ways to deal with these types of arguments. The three most common are: Using sys.argv Using getopt module/li> Using argparse module The Python sys module allows access to command-line argument
4 min read
How to pass multiple arguments to function ?
A Routine is a named group of instructions performing some tasks. A routine can always be invoked as well as called multiple times as required in a given program.  When the routine stops, the execution immediately returns to the stage from which the routine was called. Such routines may be predefined in the programming language or designed or imple
5 min read
Python | Set 6 (Command Line and Variable Arguments)
Previous Python Articles (Set 1 | Set 2 | Set 3 | Set 4 | Set 5) This article is focused on command line arguments as well as variable arguments (args and kwargs) for the functions in python. Command Line Arguments Till now, we have taken input in python using raw_input() or input() [for integers]. There is another method that uses command line arg
2 min read
Python | Passing dictionary as keyword arguments
Many times while working with Python dictionaries, due to advent of OOP Paradigm, Modularity is focussed in different facets of programming. Hence there can be many use cases in which we require to pass a dictionary as argument to a function. But this required the unpacking of dictionary keys as arguments and it's values as argument values. Let's d
3 min read
Python: Passing Dictionary as Arguments to Function
A dictionary in Python is a collection of data which is unordered and mutable. Unlike, numeric indices used by lists, a dictionary uses the key as an index for a specific value. It can be used to store unrelated data types but data that is related as a real-world entity. The keys themselves are employed for using a specific value. Refer to the belo
2 min read
Tuple as function arguments in Python
Tuples have many applications in all the domains of Python programming. They are immutable and hence are important containers to ensure read-only access, or keeping elements persistent for more time. Usually, they can be used to pass to functions and can have different kinds of behavior. Different cases can arise. Case 1: fnc(a, b) - Sends a and b
2 min read
How to handle invalid arguments with argparse in Python?
Argparse module provides facilities to improve the command-line interface. The methods associated with this module makes it easy to code for command-line interface programs as well as the interaction better. This module automatically generates help messages and raises an error when inappropriate arguments are passed. It even allows customizing the
4 min read
Unpacking arguments in Python
If you have used Python even for a few days now, you probably know about unpacking tuples. Well for starter, you can unpack tuples or lists to separate variables but that not it. There is a lot more to unpack in Python. Unpacking without storing the values: You might encounter a situation where you might not need all the values from a tuple but you
3 min read
Pass function and arguments from node.js to Python
Prerequisites: How to run python scripts in node.js using the child_process module. In this article, we are going to learn how to pass functions and arguments from node.js to Python using child_process. Although Node.js is one of the most widely used web development frameworks, it lacks machine learning, deep learning, and artificial intelligence l
4 min read
Packing and Unpacking Arguments in Python
We use two operators * (for tuples) and ** (for dictionaries). Background Consider a situation where we have a function that receives four arguments. We want to make a call to this function and we have a list of size 4 with us that has all arguments for the function. If we simply pass a list to the function, the call doesn't work. C/C++ Code # A Py
5 min read
Pass Arguments to the Metaclass from the Class in Python
Metaclasses in Python provide a powerful way to control the creation and behavior of classes. They act as the "class of a class" and allow you to customize class creation and behavior at a higher level. One interesting aspect of metaclasses is the ability to pass arguments from a class to its metaclass during the class definition. What is a Metacla
3 min read
Command Line Arguments in Python
The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of arguments. The three most common are:  Using sys.argvUsing getopt moduleUsing argparse moduleUsing sys.argvThe sys module provides functions and
5 min read
Default arguments in Python
Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value. Default Arguments: Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is
7 min read
Deep dive into Parameters and Arguments in Python
There is always a little confusion among budding developers between a parameter and an argument, this article focuses to clarify the difference between them and help you to use them effectively. Parameters:A parameter is the variable defined within the parentheses during function definition. Simply they are written when we declare a function. Examp
3 min read
How to find the number of arguments in a Python function?
In this article, we are going to see how to count the number of arguments of a function in Python. We will use the special syntax called *args that is used in the function definition of python. Syntax *args allow us to pass a variable number of arguments to a function. We will use len() function or method in *args in order to count the number of ar
3 min read
How to fix Python Multiple Inheritance generates "TypeError: got multiple values for keyword argument".
Multiple inheritance in Python allows a class to inherit from more than one parent class. This feature provides flexibility but can sometimes lead to complications, such as the error: "TypeError: got multiple values for keyword argument". This error occurs when the method resolution order (MRO) leads to ambiguous function calls, especially with key
5 min read
Core arguments in serializer fields - Django REST Framework
Serializer fields in Django are same as Django Form fields and Django model fields and thus require certain arguments to manipulate the behaviour of those Fields. In Django REST Framework the very concept of Serializing is to convert DB data to a datatype that can be used by javascript. This article revolves around various arguments that serializer
6 min read
Functions that accept variable length key value pair as arguments
To pass a variable-length key-value pair as an argument to a function, Python provides a feature called **kwargs.kwargs stands for Keyword arguments. It proves to be an efficient solution when one wants to deal with named arguments in their function. Syntax: def functionName(**anything): statement(s) Note: adding '**' to any term makes it a kwargs
2 min read
How to Pass Arguments to Tkinter Button Command?
When a user hits the button on the Tkinter Button widget, the command option is activated. In some situations, it's necessary to supply parameters to the connected command function. In this case, the procedures for both approaches are identical; the only thing that has to vary is the order in which you use them. Method 1: Pass Arguments to Tkinter
2 min read
How to pass arguments to shell script in crontab ?
In this article, we will discuss how to schedule shell scripts in crontab and to pass necessary parameters as well. First, let's create a simple script that we will be scheduled to run every 2 minutes. The below is a simple script that calculates the sum of all parameters passed and prints them to STDOUT along with the time the script was run. #! /
2 min read
Passing URL Arguments in Flask
In this article, we will cover how to Pass URL Arguments in Flask using Python. URL converters in Flask are mentioned in angular brackets (<>). These unique converters are designed to let us generate extremely dynamic URLs, where a part of the URL is regarded as a variable. For that we have created three different endpoints to understand thre
4 min read
Get the number of Explicit Arguments in the Init of a Class
In Python, the __init__ method is used for initializing a newly created object. It typically contains parameters that set the initial state of an object. To count the number of explicit arguments in the __init__ method of a class, we can use the inspect module from Python's standard library. In this article, we will see how we can get the number of
3 min read
Print Single and Multiple variable in Python
In this discussion, we will delve into the concept of variables and learn how to output both single and multiple variables in Python. What is a Variable?Variables are containers in Python that store values. In Python, You can work with both single and multiple variables. By understanding the difference of variables you can write an optimized code.
4 min read
Python | Iterate over multiple lists simultaneously
Iterating over single lists, refers to using for loops for iteration over a single element of a single list at a particular step whereas in iterating over multiple lists simultaneously, we refer using for loops for iteration over a single element of multiple lists at a particular step. Iterate over multiple lists at a time For better understanding
4 min read
Break a long line into multiple lines in Python
Break a long line into multiple lines, in Python, is very important sometime for enhancing the readability of the code. Writing a really long line in a single line makes code appear less clean and there are chances one may confuse it to be complex. Example: Breaking a long line of Python code into multiple lines Long Line: a = 1 + 2 + 3 + 4 - 5 * 2
4 min read
Python | Write multiple files data to master file
Given a number of input files in a source directory, write a Python program to read data from all the files and write it to a single master file. Source directory contains n number of files, and structure is same for all files. The objective of this code is to read all the files one by one and then append the output into a single master file having
2 min read
Python dictionary with keys having multiple inputs
Prerequisite: Python-Dictionary. How to create a dictionary where a key is formed using inputs? Let us consider an example where have an equation for three input variables, x, y, and z. We want to store values of equation for different input triplets. Example 1: C/C++ Code # Python code to demonstrate a dictionary # with multiple inputs in a key. i
4 min read
Opening multiple color windows to capture using OpenCV in Python
OpenCV is an open source computer vision library that works with many programming languages and provides a vast scope to understand the subject of computer vision.In this example we will use OpenCV to open the camera of the system and capture the video in two different colors. Approach: With the libraries available in OpenCV-Python below we will op
2 min read
Python Program to Convert a list of multiple integers into a single integer
Given a list of integers, write a Python program to convert the given list into a single integer. Examples: Input : [1, 2, 3] Output : 123 Input : [55, 32, 890] Output : 5532890 There are multiple approaches possible to convert the given list into a single integer. Let's see each one by one. Approach #1 : Naive Method Simply iterate each element in
4 min read
Practice Tags :