python替换字母为※

img


text = input("Please enter your text: ")
target_letter = input("Please enter the target_letter: ")
接下来呢

def func():
    text = input("Please enter your text: ")
    target_letter = input("Please enter the target_letter: ")
    updated_text = text.replace(target_letter, '*')
    print("Updated text: " + updated_text)


if __name__ == '__main__':
    func()

可以先把获取的输入字符串转为列表,以方便修改其相应字母;然后遍历这个列表,如果列表当前字母是要修改的字母,就把他替换为星号;然后把列表转为字符串,以打印结果。代码如下:
参考链接:
Python input() 函数 | 菜鸟教程
解决python中“TypeError ‘str‘ object does not support item assignment”问题_小鹏酱的博客-CSDN博客
python怎么将列表转为字符串-Python教程-PHP中文网

text = input("Please enter your text: ") #https://www.runoob.com/python/python-func-input.html
target_letter = input("Please enter the target_letter: ")

#https://blog.csdn.net/scp_6453/article/details/107866043
text = list(text) #把输入获取的字符串转为列表方便修改里面相应字母


for i  in range(len(text)):  #遍历列表
    
    if text[i] == target_letter: #如果当前字母是要替换的字母,则把这个字母修改为星号*
        text[i] = '*'

#https://m.php.cn/article/475777.html
result = ''.join(text)  #把修改后的列表转换为字符串,然后打印      
print("Updated text:", result)

img