Letter replacer - suggestions?

Bischoop Bischoop at vimart.net
Fri Dec 11 20:48:45 EST 2020


On 2020-12-07, MRAB <python at mrabarnett.plus.com> wrote:

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

Yes, I've learn already that should use only when want to use variables
I just started using this new f' string method.

>
> There's no need for the f prefix.
>
> > print(f' Your word to change: ,{word}')
>
> Is the comma a typo?
>

lol mixing old and new methods, just old habit.

>
> > change_this_list = list(change_this)
> > replace_with_list = list(replace_with)
> >
> > while True:
> >     try:
> >         for element in word_list:
> >             for x in change_this_list:
> >                 if element == x:
> >                     to = word_list.index(element)
> >                     replace = change_this_list.index(x)
> >                     word_list[to] = replace_with_list[replace]
> >         new_word = ''.join(word_list)
> >         print(f'{new_word}')
>
>
> You can make it a lot shorter and faster by using a dict. The entire 
> while loop section can be replaced with:
>
> replacements = dict(zip(change_this, replace_with))
> new_word = ''.join(replacements.get(letter, letter) for letter in word)
> print(new_word


Simply and nice, I wouldn't come with that yet at all, beyond my
knowledge. Thanks a lot for feedback and suggestions.




More information about the Python-list mailing list