gokyori gokyori - 4 years ago 85
Python Question

python 'print' statement and range

I didn't understand the following two cases:

from __future__ import print_function
if __name__ == '__main__':
n = int(raw_input())
print(*range(1,n+1), sep='')


if n is 3

output:123


And in second case with statement,

print(range(1,n+1), sep='')

output:[1, 2, 3]


Didn't understand the function of "*" , is it related to range or print statement?

Answer Source

It is the Argument Unpacking feature in Python.

Here is a quick example:

def f(a,b,c):
    print(a,b,c)

f(1,2,3)
f(*[1,2,3])

So print(*(1,2,3)) is equivalent to print(1,2,3)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download