Mask a card number
Support staff can see the last four digits of a card and nothing else.
- Every character except the last four becomes an asterisk.
- Anything four characters or shorter comes back untouched — there is nothing safe to hide.
mask_card(card_no: string) → string
Where you start
def mask_card(card_no: str) -> str:
Worked examples
| Call | Result |
|---|---|
mask_card("4506349012345678") | "************5678" |
mask_card("12345") | "*2345" |
mask_card("1234") | "1234" |
mask_card("") | "" |
Hint
Build the asterisks from the length, then glue on the tail.
Reference solution in Python
def mask_card(card_no: str) -> str:
if len(card_no) <= 4:
return card_no
return '*' * (len(card_no) - 4) + card_no[-4:]