Drill

ProblemsPython › payments

Mask a card number

easypaymentsPython

Support staff can see the last four digits of a card and nothing else.

mask_card(card_no: string) → string

Solve it in the editor →

Where you start

def mask_card(card_no: str) -> str:
    

Worked examples

CallResult
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:]

The same problem in another language

More payments problems in Python