![]() |
VOOZH | about |
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:
Hello from geeks for geeks, 25
Note: Calling the above code with no arguments or just one argument generates an error.
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.
Function arguments can have default values in Python. We provide a default value to an argument by using the assignment operator (=). Example:
Hello from gfg, 25 Hello from gfg, 26
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.
hello from gfg , 25
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.
hello from gfg , 25
.format() method replaces {0}, {1} placeholders with values in the order they are provided. This allows for structured formatting without converting values to strings.
hello from gfg , 25
Instead of using positional indexes, named placeholders improve readability. {key} placeholders match keyword arguments provided inside .format().
hello from gfg , 25
To manually concatenate values, convert non-string arguments to strings using str(). This method is simple but requires converting non-string data to string.
hello from gfg , 25
f-strings allow embedding expressions inside string literals using {}. They provide a concise and readable way to format strings dynamically.
hello from gfg , 25
*args parameter allows passing multiple arguments dynamically and the function processes them as a tuple, making it flexible for handling multiple inputs.
['Hello from', 'geeks', 25] ['Hello', 'gfg', 26]
The **kwargs parameter allows passing multiple keyword arguments as a dictionary. The function iterates over key-value pairs and prints them.
name geeks n - 25 name best n - 26