Meaning of adding an asterisk before a variable in python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Meaning of adding an asterisk before a variable in python

In the code below the variable c has an asterisk before it. Printing the variable c returns a list, similarly checking it's type also return class list. what exactly is the function of asterisk here?. *c,d="2","3" print(d) print(c) print(type(d)) print(type(c)) 3 ['2'] <class 'str'> <class 'list'>

15th Jun 2018, 9:27 AM
Sujith
1 Answer
+ 9
in simple words. * here means "take everything what is not taken". your example is not that obvious. Assume we have such a snippet: a, *b, c = 1, 2,3,4,5,8 here a takes the fitst value from the right part, c = the last value, and *b is interpreted the last and takes everythng left as a list: >>> a, *b, c = 1, 2,3,4,5,8 >>> a 1 >>> c 8 >>> b [2, 3, 4, 5] >>> print(*b) 2 3 4 5
15th Jun 2018, 12:22 PM
strawdog
strawdog - avatar