How to Print Multiple Arguments in Python? (original) (raw)

Last Updated : 04 Mar, 2025

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. At the time of function call each argument is always assigned to the parameter in the function definition. Example:

Python `

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

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

`

Output

Hello from geeks for geeks, 25

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

Variable Function Arguments

In the above example, the function had a fixed number of arguments but in Python, there are other ways to define a function that can take the variable number of arguments. Let’s look at some ways to do this.

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

Using Tuple Formatting

The % operator allows inserting values into a string by using placeholders (%s for strings, %d for integers, etc.). The values to be inserted are passed as a tuple after the % operator and it follows a strict ordering of arguments.

Python `

def GFG(name, num): print("hello from %s , %s" % (name, num))

GFG("gfg", "25")

`

Output

hello from gfg , 25

Using Dictionary Formatting

This method works similarly to tuple formatting but allows referencing values using dictionary keys. Placeholders use %(key)s, which get replaced by the corresponding dictionary values.

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 .format() with Positional Arguments

.format() method replaces {0}, {1} placeholders with values in the order they are provided. This allows for structured formatting without converting values to strings.

Python `

def GFG(name, num): print("hello from {0} , {1}".format(name, num))

GFG("gfg", "25")

`

Output

hello from gfg , 25

Using .format() with Named Arguments

Instead of using positional indexes, named placeholders improve readability. {key} placeholders match keyword arguments provided inside .format().

Python `

def GFG(name, num): print("hello from {n} , {r}".format(n=name, r=num))

GFG("gfg", "25")

`

Output

hello from gfg , 25

Using String Concatenation

To manually concatenate values, convert non-string arguments to strings using str(). This method is simple but requires converting non-string data to string.

Python `

def GFG(name, num): print("hello from " + str(name) + " , " + str(num))

GFG("gfg", "25")

`

Output

hello from gfg , 25

Using f-strings (Python 3.6+)

f-strings allow embedding expressions inside string literals using {}. They provide a concise and readable way to format strings dynamically.

Python `

def GFG(name, num): print(f'hello from {name} , {num}')

GFG("gfg", "25")

`

Output

hello from gfg , 25

Using *args:

*args parameter allows passing multiple arguments dynamically and the function processes them as a tuple, making it flexible for handling multiple inputs.

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:

The **kwargs parameter allows passing multiple keyword arguments as a dictionary. The function iterates over key-value pairs and prints them.

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