Python Strings · Question 16 · Hard

A slice with negative start and negative step is taken from a string. What is printed?

s = "abcdef"
print(s[-2:1:-1])
  1. edcb
  2. fedc
  3. ed
  4. edc
Answer and explanation

Answer: D. edc

Explanation: Index -2 is 'e' (index 4). With step -1 the slice moves left and stops before index 1, so it takes indexes 4, 3 and 2: 'e', 'd', 'c'.

Previous: A slice runs past the end of a six-letter string. What does this print? · Next: split() and rsplit() are each limited to one split. What does this print?

Practise python strings

Try a fresh set of python strings questions with an explanation after every answer, free and without signing up: Python Strings practice. Or sit a full placement mock test.

More python strings questions

More python practice

Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope · Comprehensions & Generators · Classes & Objects · Exceptions & Files · Built-ins & Standard Library

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge