KnowledgeBoat Logo
|

Robotics & Artificial Intelligence

What will be the output of the Python code given below?

x = "BOARD EXAMINATION"
print(x[0:4:2])
  1. BOAR
  2. BA
  3. OR
  4. Error

Python String Manipulation

2 Likes

Answer

BA

Reason — The string "BOARD EXAMINATION" is stored in variable x. Python uses 0-based indexing.

Index Table:

IndexCharacter
0B
1O
2A
3R
4D
5(space)
6E
7X
8A
9M
10I
11N
12A
13T
14I
15O
16N

The slice x[0:4:2] means:

  • Start from index 0
  • Go up to index 4 (excluded)
  • Take every 2nd character (step = 2)

So selected indices:
→ 0 → B
→ 2 → A

Thus, the output is BA.

Answered By

1 Like


Related Questions