Open In App

Python - Star or Asterisk operator ( * )

Last Updated : 25 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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
mul = 5 * 7
print (mul)

Output
35

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)

Output
125

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) 

Output
sunday 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))

Output
41

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')

Output
cherry 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)

Output
cherry 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.

Next Article
Article Tags :
Practice Tags :

Similar Reads