What will be the output of following? x = ‘abcd’ for i in range(len(x)): print(x[i]) Select one: None of them error a b c d a
Answers
Answered by
1
Answer:
a
b
c
d
Step-by-step explanation:
range(len(x)) evaluates to range(0,4), i.e. 0, 1, 2, 3 (recall that the end of the range is exclusive). Therefore, the for loop iterates over values 0, 1, 2, 3, setting i to the currently iterated value.
Now, in the loop body, the character in the string x at the index i is printed on each iteration.
When i is
0 — it prints x[0] → 'a'
1 — it prints x[1] → 'b'
2 — it prints x[2] → 'c'
3 — it prints x[3] → 'd'
And that explains the output.
Similar questions