Mask Sensitive Output

Easy ⏱ 8 min 77% acceptance ★★★★☆ 4.3
Write a function mask_card(number) that takes a card-number string number and returns it with all but the last 4 digits replaced by *, e.g. "1234567812345678" becomes "************5678". This is a typical "safe display" formatting task before printing account info to a console/log.

Examples

Example 1
Input
mask_card("1234567812345678")
Output
'************5678'
Explanation

Last 4 digits kept, rest masked.

Example 2
Input
mask_card("1234")
Output
'1234'
Explanation

Fewer than 5 digits: nothing to mask.

Constraints

  • number is a digit-only string.
  • If the string has 4 or fewer characters, return it unchanged.

Topics

Input & Outputstring slicing

Companies

JPMorgan ChaseDeutsche Bank

Hints

Hint 1

Slice the last 4 characters with number[-4:].

Hint 2

Prefix with "*" repeated (len(number) - 4) times, but not below 0.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.