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
The code
message[-2::-2]
starts from the second-last (-2) character of the string "World Peace", which is "c".It moves backwards in steps of 2 to select every second character going towards the start of the string.
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".
The output is "ce lo".
Answered By
8 Likes
Related Questions
Which of the following expressions evaluates to False ?
- not(True) and False
- True or False
- not(False and True)
- True and not(False)
What is the output of the expression ?
country = 'International' print(country.split("n"))
('I', 'ter', 'atio', 'aI')
['I', 'ter', 'atio', 'al']
['I', 'n', 'ter', 'n', 'atio', 'n', 'al']
Error
What will be the output of the following code ?
tuple1 = (1, 2, 3) tuple2 = tuple1 tuple1 += (4, ) print(tuple1 == tuple2)
- True
- False
- tuple1
- Error
If my_dict is a dictionary as defined below, then which of the following statements will raise an exception ?
my_dict = {'apple' : 10, 'banana' : 20, 'orange' : 30}
- my_dict.get('orange')
- print(my_dict['apple', 'banana'])
- my_dict['apple'] = 20
- print(str(my_dict))