Python Function: Printing arguments with *args
1. Arbitrary Arguments Printer
Write a Python function that takes an arbitrary number of arguments using *args and prints each argument. Use ... to represent unspecified arguments.
Sample Solution:
Code:
# Define the ellipsis constant
var1 = ...
def print_arguments(*args):
"""
Prints each argument passed to the function, using var1 for unspecified arguments.
Args:
*args: Any number of positional arguments.
"""
for i, arg in enumerate(args):
print(f"Argument {i + 1}: {arg}")
if not args:
print(f"\nUnspecified arguments: {var1}")
# Example usage:
print_arguments(1, 2, 3, "Python")
print_arguments(50, [1, 2, 3], "Derya Nidia", "Mitul Calixta")
print_arguments() # Unspecified arguments: Ellipsis
Output:
Argument 1: 1 Argument 2: 2 Argument 3: 3 Argument 4: Python Argument 1: 50 Argument 2: [1, 2, 3] Argument 3: Derya Nidia Argument 4: Mitul Calixta Unspecified arguments: Ellipsis
In this code, 'var1' is defined as ..., and it is used to represent unspecified arguments when there are none provided to the function. If there are no arguments passed to the function, the code will print "Unspecified arguments: Ellipsis".
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python function that accepts any number of arguments using *args and prints each argument on a separate line, ignoring those that are Ellipsis.
- Write a Python program to collect arbitrary arguments via *args, then return a tuple containing only the non-ellipsis arguments.
- Write a Python script that accepts *args and prints the type of each argument; if an argument equals Ellipsis, print "Undefined".
- Write a Python function that takes *args, counts how many arguments are provided, and prints "No arguments" if none are given, otherwise prints all values.
Python Code Editor :
Previous: Python Extended Data Type ellipsis (...) Exercises Home.
Next: Python Function: Calling functions with flexible arguments.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.