Question from coding bat practice | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Question from coding bat practice

My question: why is the result += str[:i+1] instead of result += str[:i] from my understanding #when i = 0 result = str[0:0] = C #when i = 1 result = C + str[0:1] = C + CO #when i =2 result = C + CO + str [0:2] = C + CO + COD #when i = 3 result = C + CO + COD + str[0:3] result= CCOCODCODE https://code.sololearn.com/c3Djr94rOEk3/?ref=app

8th Mar 2020, 4:38 PM
Xvitrix
2 Answers
+ 5
range(x) generates numbers from 0 up to x-1 (x is not included) When you slice the string, str[:i] takes the indexes from 0 up to i-1 (i is not included and remember index starts with 0)
8th Mar 2020, 5:10 PM
Tibor Santa
Tibor Santa - avatar
+ 1
C: 0 O: 1 D: 2 E: 3 range(x) = 0, 1, 2, 3 result = result + str(:i) means i is not included in order to include i , str(:i+1)
9th Mar 2020, 6:35 AM
Xvitrix