+ 1

Python for loop

s = "" for i in range(3): s = s + str(i) print (s) I was fiddling around and found that this printed 012 but i dont completely understand why, could someone please explain it?

1st Dec 2020, 5:07 PM
Sayori Utsugj
Sayori Utsugj - avatar
1 Respuesta
+ 10
This will run the loop from 0 to 2. Range, by default, starts running from 0 until n-1. So, you are starting loop from 0 to 2, and hence appending each generated digit to s. In first run, s = ''+'0' In second, s ='0'+'1' In third, s = '01'+'2' At the end, s = '012'
1st Dec 2020, 5:10 PM
Charitra
Charitra - avatar