Skip to content
Advertisement

Replace All Specific Characters in String using Python

I have a problem with a function I am trying to implement that needs to replace some letters (in a given string), for some other characters, defined on a dictionary.

I have this dictionary:

JavaScript

I want to pass a string that when a character matches any key from the dictionary (case insensitive match), it tries to save all the possible combinations for that letter, using the values on the dictionary.

Here’s what I currently have:

JavaScript

In the current code, I have only replaced a character on the string. I need to change it so that it matches the output below,

The desired output, using the word sopa, should be like the following:

JavaScript

I appreciate any help. Thank y’all in advance.

Advertisement

Answer

You were on the right track. Here is your modified function without recursion or itertools. This code also allows the function to take either a single word or a list of words.

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement