Angel Vergina
Angel Vergina

Reputation: 11

how to write a python code to mask the input as * in the output

Input ABC123 Output ***123 The first half of the characters in the word should contain only alphabetic in the upper case. The second half of the words should contain only digits
While displaying the output all alphabetic should be masked with *.

Upvotes: 1

Views: 1511

Answers (3)

Alain T.
Alain T.

Reputation: 42143

You could use a translation table:

maskUppercase = str.maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ","*"*26)

string = "ABCD1234"
print(string.translate(maskUppercase)) 

****1234

you could also reform the string using join() after masking the letters in a list comprehension:

"".join([c,"*"][c.lower()!=c] for c in string) 

Upvotes: 0

Leo Arad
Leo Arad

Reputation: 4472

you can use

import re

input_str = "ABC123"
print(re.sub("[A-Z]", "*", input_str))

Output

'***123'

The regex will replace all alphabetic chars with * in a giving string

Upvotes: 2

Tristan Nemoz
Tristan Nemoz

Reputation: 2048

You can use the string module to do this.

from string import ascii_uppercase

input = "ABC123"
output = "".join("*" if x in ascii_uppercase else x for x in input)

Upvotes: 0

Related Questions