Robotics & Artificial Intelligence

What will be the output of the following Python code?

str1 = 'PYTHON'
print(str1[1:4])

Python String Manipulation

1 Like

Answer

YTH

Working

The string 'PYTHON' is stored in the variable str1. Python uses 0-based indexing.

Index Table:

IndexCharacter
0P
1Y
2T
3H
4O
5N

The slice str1[1:4] selects characters from index 1 to 3 (excluding 4): YTH

Hence, the output is YTH.

Answered By

1 Like


Related Questions