ProgrammingHardProduct-company MCQ pattern

What is printed?

What is printed?
s = 'abcdefgh'
print(s[::-2], s[1::3], s[-3:-6:-1], s[::-1][::2])
  1. A. hfdb beh fed hfdb
  2. B. hfdb beh def hfdb
  3. C. hfdb ehe fed hfdb
  4. D. hgdb beh fed hfdb

Correct answer: A. hfdb beh fed hfdb

s[::-2] takes every second character from the end, s[1::3] takes indices 1, 4, 7, and s[-3:-6:-1] walks backward from index -3 down to -5. Reversing and then taking every second character gives the same result as the first slice.

More Python Programming questions

Exams that ask this topic