1

I'm new to python programming and solving some problems in Hacker Rank.. the question was to read an interget N and print in the format of 123....N without using any string methods. I did the following

Answer = ''

for i in range(1,Input+1):
    Answer += str(i)

print Answer

When i looked into the other solution one of the solution given was

print(*range(1,N+1), sep='')

I tried to understand this but could not. I could not find any documentation for *range. can someone pls help..

VijayS
  • 81
  • 4

2 Answers2

1

The given expression is calling the range() built-in function, plus the unpacking arguments operator * (a.k.a. the "splat" operator), that simply expands a sequence into its individual elements. For example:

print(*range(1, 5+1), sep='')

Will expand to this:

print(1, 2, 3, 4, 5, sep='')
Óscar López
  • 232,561
  • 37
  • 312
  • 386
-1

You can store the iteration results in a list and remove the comma and print it :

res = ()
for i in range(1,n+1):
    res+=(i,)
    fin = ''.join(map(str,res))
print(fin)