Robotics & Artificial Intelligence

What does the following code output?

string = "Hello World"
print(string[1:5])
  1. "Hell"
  2. "ello"
  3. "World"
  4. "Hello"

Python String Manipulation

1 Like

Answer

"ello"

Reason — In Python, string slicing returns characters from the starting index up to the ending index minus one. Therefore, string[1:5] extracts the characters at index positions 1, 2, 3, and 4, which are e, l, l, and o, and hence the output is "ello".

Answered By

1 Like


Related Questions