0
Python - What's the meaning of * in a print() statement?
zzz = [1,2,3,4,5] print(zzz, *zzz) # outputs [1, 2, 3, 4, 5] 1 2 3 4 5 It seems that the * in the print() statement produces an output that is without the pair of brackets in the defined list, dictionary, set or tuple variable. In addition, commas are removed and there exists a space between the items. What's the meaning of * in a print() statement? Why are brackets (ie. (),{}, or []) removed and spaces added?
3 odpowiedzi
0
Solus
Help on print:
print(*text, end="\n", sep=" ", file=sys.stdout)
Help on *:
*(1,2,3,4,5) = 1,2,3,4,5
*[1,2,3,4,5] = 1,2,3,4,5
*{1,2,3,4,5} = 1,2,3,4,5
Help on **:
**{"name":"user", "addr":None} -> (returns) name="user", addr=None
Doing *zzz = 1,2,3,4,5
so, print(zzz, *zzz) -> print([1,2,3,4,5], 1,2,3,4,5)
And sep=" ", so it will print: "[1,2,3,4,5] 1 2 3 4 5"
• * is used in some functions to let user pass a lot of args and will return a list
• ** is used in some functions to let user pass a lot of keyword-args and will return dict