ProgrammingModerateTCS NQT · Infosys pattern

What is printed?

What is printed?
s = "abcdefgh"
print(s[1:6:2], s[::-3], s[-3:-1], len(s[5:2]))
  1. A. bdf hea fg 0
  2. B. bdf heb fg 0
  3. C. bdf heb fgh 0
  4. D. ace heb fg 0

Correct answer: B. bdf heb fg 0

s[1:6:2] takes indices 1, 3, 5 → bdf. s[::-3] starts at the last index 7 and steps back by 3: h (7), e (4), b (1). s[-3:-1] covers indices 5 and 6 → fg (the stop is excluded). A forward slice with start > stop is simply empty – no error.

More Python Programming questions

Exams that ask this topic