Letter replacer - suggestions?

Marco Sulla Marco.Sulla.Python at gmail.com
Mon Dec 7 17:16:36 EST 2020


Not sure why you want to do this (it's schoolwork)? Anyway, this is my version:

word = input('input word you want to change letters in: ')

chars = tuple(word)
change_this = input('Enter the letters you want to change: ')
replace_with = input('Enter the letters to replace with: ')

if len(change_this) != len(replace_with):
    raise RuntimeError(
        "Letters to replace must be equals in number to letters you want " +
        "to change"
    )

change_chars = tuple(change_this)
replace_chars = tuple(replace_with)

new_chars = []

for ch in chars:
    try:
        i = change_chars.index(ch)
    except ValueError:
        new_chars.append(ch)
    else:
        new_chars.append(replace_chars[i])


More information about the Python-list mailing list