Robotics & Artificial Intelligence
What will be the output of the Python code given below?
x = "BOARD EXAMINATION"
print(x[0:4:2])
- BOAR
- BA
- OR
- Error
Python String Manipulation
1 Like
Answer
BA
Reason — The string "BOARD EXAMINATION" is stored in variable x. Python uses 0-based indexing.
Index Table:
| Index | Character |
|---|---|
| 0 | B |
| 1 | O |
| 2 | A |
| 3 | R |
| 4 | D |
| 5 | (space) |
| 6 | E |
| 7 | X |
| 8 | A |
| 9 | M |
| 10 | I |
| 11 | N |
| 12 | A |
| 13 | T |
| 14 | I |
| 15 | O |
| 16 | N |
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
3 Likes
Related Questions
Give the output of the following Python code:
msg = "Artificial Intelligence" print(msg[11:14])- tel
- Int
- ell
- gen
The keyword used to include a package or module in a Python program is:
- export
- include
- collect
- import
Mention any two sensors used in autonomous drones.
'In a hospital, cobots assist nurses in delivering medicines to patients.'
Give any two advantages of using cobots in hospitals.