Why is the output c and d | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is the output c and d

i expect the putput to be c and e. x = ("a", "b", "c", "d", "e") print x[2:4]

28th Oct 2017, 6:11 AM
stephen haokip
stephen haokip - avatar
2 Answers
+ 2
because computers start counting from zero(0) so x[2:4] means start at 2 all the way to 4 but NOT including 4. "a" at index [0] "b" at index [1] "c" at index [2]------> start "d" at index [3]-----> middle "e" at index[4]------> end (not including index 4) To print c to d you need to do : print(x[2:5]
28th Oct 2017, 6:23 AM
jay
+ 1
crystal clear thank you
28th Oct 2017, 7:47 AM
stephen haokip
stephen haokip - avatar