Open In App

How to Print Multiple Arguments in Python?

Last Updated : 29 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

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

Python3




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:

Python3




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
     

Python3




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


Output:

hello from gfg , 25

  • Pass it as a dictionary
     

Python3




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
     

Python3




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
     

Python3




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


Output:

hello from gfg , 25

  • Concatenate strings
     

Python3




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
     

Python3




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


Output:

hello from gfg , 25

  • Using *args 

Python3




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 

Python3




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



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 implem
4 min read
How to Run Another Python script with Arguments in Python
Running a Python script from another script and passing arguments allows you to modularize code and enhance reusability. This process involves using a subprocess or os module to execute the external script, and passing arguments can be achieved by appending them to the command line. In this article, we will explore different approaches to Running a
3 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
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
5 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
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.argv The sys module provides functions and
4 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
Practice Tags :