How to Print Multiple Arguments in Python?
Last Updated :
10 Jul, 2024
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
Python
def GFG(name, num):
print("hello from %s , %s" % (name, num))
GFG("gfg", "25")
Output:
hello from gfg , 25
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
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
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]
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
Please Login to comment...