Python Strings · Question 15 · Medium

A slice runs past the end of a six-letter string. What does this print?

s = "Mumbai"
print(len(s[10:]), s[2:100])
  1. 0 mbai
  2. IndexError
  3. 0 mb
  4. 6 mbai
Answer and explanation

Answer: A. 0 mbai

Explanation: Slices never raise IndexError; out-of-range bounds are simply clipped to the string. s[10:] is the empty string, with length 0, and s[2:100] runs from index 2 to the end: "mbai".

Previous: Which statement about Python strings is true? · Next: A slice with negative start and negative step is taken from a string. What is…

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