Open In App

Python – Star or Asterisk operator ( * )

Last Updated : 25 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

There are a many places you’ll see * and ** used in Python. Many Python Programmers even at the intermediate level are often puzzled when it comes to the asterisk ( * ) character in Python. 

After studying this article, you will have a solid understanding of the asterisk ( * ) operator in Python and become a better coder in the process!

Below are the various uses of the asterisk ( * ) operator in Python:

  • Multiplication :
    In Multiplication, we multiply two numbers using Asterisk /  Star Operator as infix an Operator.

Python3




# using asterisk
mul = 5 * 7
print (mul)


Output:

35
  • Exponentiation :
    Using two(**) Star Operators we can get the exponential value of any integer value.

Python3




a = 5
b = 3
 
# using asterisk
result = a ** b
print(result)


Output:

125
  • Multiplication of a list :
    With the help of  ‘ * ‘ we can multiply elements of a list, it transforms the code into single line.

Python3




# using asterisk
list = ['geeks '] * 3
 
print(list)


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 them.  

Python3




arr = ['sunday', 'monday', 'tuesday', 'wednesday']
 
# without using asterisk
print(' '.join(map(str,arr)))
 
# using asterisk
print (*arr)


Output:

sunday monday tuesday wednesday
sunday monday tuesday wednesday
  • 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.

Python3




# using asterisk
def addition(*args):
  return sum(args)
 
print(addition(5, 10, 20, 6))


Output:

41
  • 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
     

Python3




# using asterisk
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

Just another example of using **kwargs, for much better understanding.

Python3




# using asterisk
def food(**kwargs):
  for items in kwargs:
    print(f"{kwargs[items]} is a {items}")
     
     
dict = {'fruit' : 'cherry', 'vegetable' : 'potato', 'boy' : 'srikrishna'}
# using asterisk
food(**dict)


Output:

cherry is a fruit
potato is a vegetable
srikrishna is a boy


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads