Indexing and slicing
Strings, lists and tuples are sequences. Each item has a position counted from 0, and negative positions count back from the end.
word = "python"
print(word[0], word[-1]) # p n
print(word[1:4]) # yth
print(word[:2]) # py
print(word[2:]) # thon
print(word[-3:]) # hon
print(word[::2]) # pto
print(word[::-1]) # nohtypHow a slice reads
s[start:stop:step] begins at start and stops before stop, so s[1:4] holds 4 - 1 = 3 items. Leave out start to begin at the edge, stop to run to the other edge, and step to take every item. A negative step walks backwards.
- An index past the end raises
IndexError. - A slice past the end returns whatever exists, possibly an empty string.
s[:n]is the firstnitems ands[-n:]the lastn.- A slice is a new object. Strings cannot be changed in place, so you build a new one instead.
Your task
Write mask(text) returning text with every character except the last 4 replaced by "#", leaving text of 4 characters or fewer unchanged. Write is_palindrome(word) returning whether word reads the same backwards, ignoring case.
The explanation opens here once every check passes.