![](/img/trans.png)
[英]How to write a code to get another python script output that require an input
[英]how to write a python code to mask the input as * in the output
輸入 ABC123 Output ***123 單詞的前半部分字符只能包含大寫字母。 單詞的后半部分應僅包含數字
在顯示 output 時,所有字母都應該用 * 屏蔽。
您可以使用
import re
input_str = "ABC123"
print(re.sub("[A-Z]", "*", input_str))
Output
'***123'
正則表達式將在給定字符串中用 * 替換所有字母字符
您可以使用字符串模塊來執行此操作。
from string import ascii_uppercase
input = "ABC123"
output = "".join("*" if x in ascii_uppercase else x for x in input)
您可以使用翻譯表:
maskUppercase = str.maketrans("ABCDEFGHIJKLMNOPQRSTUVWXYZ","*"*26)
string = "ABCD1234"
print(string.translate(maskUppercase))
****1234
您還可以在屏蔽列表理解中的字母后使用 join() 重新構造字符串:
"".join([c,"*"][c.lower()!=c] for c in string)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.