Computer Science

What will be the output of the following code snippet ?

message = "World Peace"
print(message[-2::-2])

Python String Manipulation

14 Likes

Answer

Output
ce lo
Explanation
  1. The code message[-2::-2] starts from the second-last (-2) character of the string "World Peace", which is "c".

  2. It moves backwards in steps of 2 to select every second character going towards the start of the string.

  3. The traversal goes as follows:

    • Index -2: Character is "c".
    • Index -4: Character is "e".
    • Index -6: Character is a space (" ").
    • Index -8: Character is "l".
    • Index -10: Character is "o".
  4. The output is "ce lo".

Answered By

8 Likes


Related Questions