25 XP

39. Mask all but the last four

Beginner strings

Receipts and account pages often show only the end of a long number: ############5616. The rest is hidden so that someone looking over your shoulder can't read it.

Write a function mask(text) that takes a string and returns it with every character except the last four replaced by #:

  • Every character counts, spaces and punctuation included.
  • A text of four characters or fewer is returned as it is: there is nothing to hide.
  • An empty text gives an empty string "".

Example 1

Inputtext = "4556364607935616"
Output"############5616"
Explanation16 characters: 12 hidden, the last 4 shown.

Example 2

Inputtext = "Skippy"
Output"##ippy"
ExplanationOnly the first 2 of 6 are hidden.

Example 3

Inputtext = "abc"
Output"abc"
ExplanationUnder 4 characters: nothing to hide.

Constraints

  • text has 0 to 100 characters, of any kind.

Submit also runs 7 more tests you can't see.

Practises Strings (lesson 4).

Code
def mask(text):
    """Hide all but the last four characters of text.

    text: str, any characters; may be empty
    Returns: str, the same length, every character but the
        last four replaced by "#"
    """
    # Write your code here
    pass