Exercises
Slicing
3 tasks. Write the code, press Check, and the page runs it against a real Python interpreter.
Exercise 1Passed
Print the file extension without the dot, using a slice.
Python
filename = "report.pdf"
# print pdf
The last three characters are [-3:].
filename = "report.pdf"
print(filename[-3:])Exercise 2Passed
Print the word reversed.
Python
word = "stressed"
# print it backwards
A step of -1 walks backwards.
word = "stressed"
print(word[::-1])Exercise 3Passed
Print every second letter, starting from the first.
Python
letters = "abcdefgh"
# print aceg
The third slice number is the step.
letters = "abcdefgh"
print(letters[::2])