【问题标题】:how do I replace a specific character from an string如何替换字符串中的特定字符
【发布时间】:2021-08-31 07:58:35
【问题描述】:
def show_hidden_word(secret_word, old_letters_guessed):
    printage = '_ ' * len(secret_word)
    for i in range(len(secret_word)):
        for j in range(len(old_letters_guessed)):
            if secret_word[i] == old_letters_guessed[j]:
                printage = printage.replace('_', secret_word[i])
    print(printage)

show_hidden_word('house', ['s', 'p', 'j', 'i', 'm', 'k'])

这是我被告知要为学校做的更大的 HangMan 任务的一部分 我只是不明白如何将 printage[i] 更改为 secret_word[i]

【问题讨论】:

    标签: python-3.x string str-replace


    【解决方案1】:

    我希望这会有所帮助:

    my_string = 'hello'
    my_string = list(my_string)
    my_string[2] = 'e'
    my_string = ''.join(my_string)
    
    print(my_string)
    

    输出:

    heelo
    

    【讨论】:

    • 你看我需要得到 char 的位置,它必须来自调用函数的用户/对象
    猜你喜欢
    • 2012-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多