Robotics & Artificial Intelligence
What will be the output of the following Python code?
str1 = 'PYTHON'
print(str1[1:4])
Python String Manipulation
2 Likes
Answer
YTH
Working
The string 'PYTHON' is stored in the variable str1. Python uses 0-based indexing.
Index Table:
| Index | Character |
|---|---|
| 0 | P |
| 1 | Y |
| 2 | T |
| 3 | H |
| 4 | O |
| 5 | N |
The slice str1[1:4] selects characters from index 1 to 3 (excluding 4): YTH
Hence, the output is YTH.
Answered By
3 Likes