Python - Star or Asterisk operator ( * )
Last Updated :
25 Apr, 2025
The asterisk (*) operator in Python is a versatile tool used in various contexts. It is commonly used for multiplication, unpacking iterables, defining variable-length arguments in functions, and more.
Uses of the asterisk ( * ) operator in Python
Multiplication
In Multiplication, we multiply two numbers using Asterisk / Star Operator as infix an Operator.
Python
Exponentiation
Using two(**) Star Operators we can get the exponential value of any integer value.
Python
a = 5
b = 3
res = a ** b
print(res)
Multiplication of a list
With the help of ' * ' we can multiply elements of a list, it transforms the code into single line.
Python
a = ['geeks '] * 3
print(a)
Output['geeks ', 'geeks ', 'geeks ']
Unpacking a function using positional argument
This method is very useful while printing your data in a raw format (without any comma and brackets ). Many of the programmer try to remove comma and bracket by using a convolution of functions, Hence this simple prefix asterisk(*) can solve your problem in unpacking an iterable.
Python
arr = ['sunday', 'monday', 'tuesday', 'wednesday']
print(' '.join(map(str,arr)))
print (*arr)
Outputsunday monday tuesday wednesday
sunday monday tuesday wednesday
Explanation:
- The code defines a list arr with days of the week as strings. join(map(str, arr)) joins the elements of arr into a single string, separating them by a space.
- *arr unpacks the list and passes each element as a separate argument to the print() function, printing them with a space in between.
Passing a Function Using with an arbitrary number of positional arguments
Here a single asterisk( * ) is also used in *args. It is used to pass a variable number of arguments to a function, it is mostly used to pass a non-key argument and variable-length argument list. It has many uses, one such example is illustrated below, we make an addition function that takes any number of arguments and able to add them all together using *args.
Python
def addition(*args):
return sum(args)
print(addition(5, 10, 20, 6))
Explanation:
- The addition() function accepts a variable number of arguments using *args. It calculates the sum of all arguments passed to the function using the sum() function.
- The function is called with four numbers: 5, 10, 20, and 6.
Passing a Function Using with an arbitrary number of keyword arguments
Here a double asterisk( ** ) is also used as **kwargs, the double asterisks allow passing keyword arguments. This special symbol is used to pass a keyword arguments and variable-length argument lists. It has many uses, one such example is illustrated below
Python
def food(**kwargs):
for items in kwargs:
print(f"{kwargs[items]} is a {items}")
food(fruit = 'cherry', vegetable = 'potato', boy = 'srikrishna')
Outputcherry is a fruit
potato is a vegetable
srikrishna is a boy
Explanation:
- The food() function accepts keyword arguments using **kwargs. It loops through the keys of the kwargs dictionary. For each key-value pair, it prints the value followed by the key.
- The function is called with explicit keyword arguments: fruit = 'cherry', vegetable = 'potato', and boy = 'srikrishna'.
Just another example of using **kwargs, for much better understanding.
Python
def food(**kwargs):
for items in kwargs:
print(f"{kwargs[items]} is a {items}")
d = {'fruit' : 'cherry', 'vegetable' : 'potato', 'boy' : 'srikrishna'}
food(**d)
Outputcherry is a fruit
potato is a vegetable
srikrishna is a boy
Explanation:
- The food() function accepts keyword arguments using **kwargs. It loops through the keys of the kwargs dictionary. For each key-value pair, it prints the value followed by the key.
- The dictionary d is unpacked and passed to the function with **d.
Similar Reads
Python Operators
In Python programming, Operators in general are used to perform operations on values and variables. These are standard symbols used for logical and arithmetic operations. In this article, we will look into different types of Python operators. OPERATORS: These are the special symbols. Eg- + , * , /,
6 min read
Precedence and Associativity of Operators in Python
In Python, operators have different levels of precedence, which determine the order in which they are evaluated. When multiple operators are present in an expression, the ones with higher precedence are evaluated first. In the case of operators with the same precedence, their associativity comes int
4 min read
Python Arithmetic Operators
Python Arithmetic Operators
Python operators are fundamental for performing mathematical calculations. Arithmetic operators are symbols used to perform mathematical operations on numerical values. Arithmetic operators include addition (+), subtraction (-), multiplication (*), division (/), and modulus (%). OperatorDescriptionS
4 min read
Difference between / vs. // operator in Python
In Python, both / and // are used for division, but they behave quite differently. Let's dive into what they do and how they differ with simple examples./ Operator (True Division)The / operator performs true division.It always returns a floating-point number (even if the result is a whole number).It
2 min read
Python - Star or Asterisk operator ( * )
The asterisk (*) operator in Python is a versatile tool used in various contexts. It is commonly used for multiplication, unpacking iterables, defining variable-length arguments in functions, and more.Uses of the asterisk ( * ) operator in PythonMultiplicationIn Multiplication, we multiply two numbe
3 min read
What does the Double Star operator mean in Python?
The ** (double star)operator in Python is used for exponentiation. It raises the number on the left to the power of the number on the right. For example:2 ** 3 returns 8 (since 2³ = 8)It is one of the Arithmetic Operator (Like +, -, *, **, /, //, %) in Python and is also known as Power Operator.Prec
2 min read
Division Operators in Python
Division Operators allow you to divide two numbers and return a quotient, i.e., the first number or number at the left is divided by the second number or number at the right and returns the quotient. There are two types of division operators: Float divisionInteger division( Floor division)When an in
5 min read
Modulo operator (%) in Python
Modulo operator (%) in Python gives the remainder when one number is divided by another. Python allows both integers and floats as operands, unlike some other languages. It follows the Euclidean division rule, meaning the remainder always has the same sign as the divisor. It is used in finding even/
4 min read