Friday, September 11, 2026

Python Slicing

 Python Slicing:


1. What is the output? s = "Python" print(s[0:3])

   A. 'Pyt'

   B. 'Pyth'

   C. 'Python'

   D. 'yth'


2. What is the output? s = "Python" print(s[2:5])

   A. 'yth'

   B. 'hon'

   C. 'thon'

   D. 'tho'


3. What is the output? nums = [10, 20, 30, 40, 50] print(nums[-3:])

   A. [40, 50]

   B. [30, 40]

   C. [20, 30, 40]

   D. [30, 40, 50]


4. What is the output? s = "abcdef" print(s[::2])

   A. 'ace'

   B. 'fed'

   C. 'abcd'

   D. 'bdf'


5. What is the output? s = "Python" print(s[::-1])

   A. 'nothP'

   B. 'Python'

   C. 'Pnohty'

   D. 'nohtyP'


6. What is the output? nums = [0, 1, 2, 3, 4, 5] print(nums[1:5:2])

   A. [0, 2, 4]

   B. [2, 4]

   C. [1, 3]

   D. [1, 3, 5]


7. What is the output? s = "Python" print(s[:4])

   A. 'ytho'

   B. 'Pytho'

   C. 'Python'

   D. 'Pyth'


8. What is the output? s = "Python" print(s[2:])

   A. 'tho'

   B. 'Python'

   C. 'thon'

   D. 'ython'


9. What is the output? s = "abcdef" print(s[1:6:2])

   A. 'bcdef'

   B. 'ace'

   C. 'bde'

   D. 'bdf'


10. What is the output? s = "abcdef" print(s[-1])

   A. 'f'

   B. 'e'

   C. 'd'

   D. 'a'


11. What is the output? s = "Python" print(s[-4:-1])

   A. 'yth'

   B. 'hon'

   C. 'tho'

   D. 'ytho'


12. What is the output? nums = [1, 2, 3, 4, 5, 6] print(nums[::3])

   A. [1, 4]

   B. [2, 5]

   C. [1, 4, 6]

   D. [1, 3, 5]


13. What is the output? s = "abcdef" print(s[5:1:-1])

   A. 'fedc'

   B. 'edcb'

   C. 'fed'

   D. 'fecd'


14. What is the output? s = "abcdef" print(s[::-2])

   A. 'fdb'

   B. 'bdf'

   C. 'eca'

   D. 'fed'


15. What is the output? s = "Python" print(s[1:5:3])

   A. 'yth'

   B. 'yt'

   C. 'to'

   D. 'yo'


16. What is the output? s = "Python" print(s[::])

   A. 'ython'

   B. 'Pyth'

   C. 'Python'

   D. 'nohtyP'


17. What is the output? nums = [10, 20, 30, 40, 50] print(nums[1:-1])

   A. [30, 40]

   B. [20, 30, 40]

   C. [20, 30, 40, 50]

   D. [10, 20, 30, 40]


18. What is the output? s = "abcdef" print(s[4:1:-1])

   A. 'fed'

   B. 'cde'

   C. 'edc'

   D. 'edcb'


19. What is the output? s = "abcdefgh" print(s[2:7:2])

   A. 'bdf'

   B. 'ceg'

   C. 'cdefg'

   D. 'ce'


20. What is the output? s = "Python" print(s[-5:-2])

   A. 'tho'

   B. 'yth'

   C. 'Pyt'

   D. 'ytho'

1 comment:

perlknowledge said...

What is the output?

s = "Python" print(s\[0:3\])

Indices 0, 1, and 2 are included, while index 3 is excluded.

What is the output?

s = "Python" print(s\[2:5\])

Indices 2, 3, and 4 contain t, h, and o.

What is the output?

nums = \[10, 20, 30, 40, 50\] print(nums\[-3:\])

Negative index -3 refers to 30, and the omitted stop extends to the end.

What is the output?

s = "abcdef" print(s\[::2\])

Starting at index 0 and taking every second character gives a, c, and e.

What is the output?

s = "Python" print(s\[::-1\])

The negative step reverses the complete string.

What is the output?

nums = \[0, 1, 2, 3, 4, 5\] print(nums\[1:5:2\])

Indices 1 and 3 are selected before the exclusive stop index 5.

What is the output?

s = "Python" print(s\[:4\])

The omitted start defaults to the beginning, and index 4 is excluded.

What is the output?

s = "Python" print(s\[2:\])

Index 2 contains t, so the result begins with t.

What is the output?

s = "abcdef" print(s\[1:6:2\])

Indices 1, 3, and 5 are selected with step 2.

What is the output?

s = "abcdef" print(s\[-1\])

Negative index -1 refers to the last character.

What is the output?

s = "Python" print(s\[-4:-1\])

Negative index -4 is y, and -1 is exclusive, so y, t, and h are selected.

What is the output?

nums = \[1, 2, 3, 4, 5, 6\] print(nums\[::3\])

Starting at index 0 and stepping by 3 selects indices 0 and 3.

What is the output?

s = "abcdef" print(s\[5:1:-1\])

The descending indices include f, e, d, and c before reaching exclusive stop 1.

What is the output?

s = "abcdef" print(s\[::-2\])

The slice starts at the last character and moves backward by 2.

What is the output?

s = "Python" print(s\[1:5:3\])

Starting at index 1 and stepping by 3 reaches indices 1 and 4.

What is the output?

s = "Python" print(s\[::\])

All indices are selected because start, stop, and step are omitted.

What is the output?

nums = \[10, 20, 30, 40, 50\] print(nums\[1:-1\])

Start 1 selects 20, and stop -1 excludes the final 50.

What is the output?

s = "abcdef" print(s\[4:1:-1\])

Indices 4, 3, and 2 are selected while index 1 is excluded.

What is the output?

s = "abcdefgh" print(s\[2:7:2\])

Indices 2, 4, and 6 are selected, producing c, e, and g.

What is the output?

s = "Python" print(s\[-5:-2\])

Negative indices -5, -4, and -3 correspond to y, t, and h, with -2 excluded.