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:

chars = {
        'A': ['4', '@'],
        'E': ['3', '€', '&', '£'],
        'T': ['7'],
        'S': ['$', '§', '5'],
        'I': ['1', '!'],
        'O': ['0'],
        'B': ['8'],
        'C': ['<'],
        'L': ['1']
    }

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:

def type4 (words, v):
    chars = {
        'A': ['4', '@'],
        'E': ['3', '€', '&', '£'],
        'T': ['7'],
        'S': ['$', '§', '5'],
        'I': ['1', '!'],
        'O': ['0'],
        'B': ['8'],
        'C': ['<'],
        'L': ['1']
    }

    tmp = []
    string = []
    for w in words:                                 # each character in a word
        for key in chars.keys():                    # [A, E, T, S, I, O, B, C, L]
            for c in chars[key]:                    # [4, @, 3, €, &, £, ...]
                string = list(w)                    
                k = 0                               
                ya = ""
                for char in string:
                    if (char == key or char == key.lower()):
                        string[k] = c
                    else:
                        string[k] = char
                    k += 1
                if (not string == list(w)):
                    for i in string:
                        ya += i
                    print(ya, end="nn")

                tmp.append(ya)

    return tmp

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:

sopa
sop4
sop@
s0pa
s0p4
s0p@
$opa
$op4
$op@
$0pa
$0p4
$0p@
§opa
§op4
§op@
§0pa
§0p4
§0p@
5opa
5op4
5op@
50pa
50p4
50p@

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.

def type4 (words):
    chars = {'A': ['4', '@'],'E': ['3', '€', '&', '£'],'T': ['7'],'S': ['$', '§', '5'],
         'I': ['1', '!'],'O': ['0'],'B': ['8'],'C': ['<'],'L': ['1']}
    tmp = []
    #This allows you to take a single word or list of words as argument
    if type(words) is str:
        tmp = [words]
    elif type(words) is list:
        tmp.extend(words)

    for word in tmp:
        for letter in word:
            if letter.upper() in chars:
                for new_letter in chars[letter.upper()]:
                    new_word = word.replace(letter, new_letter)
                    tmp.append(new_word)
    return set(tmp)

print(type4("sopa"))
#{'$op@', '$opa', 's0pa', '$0pa', '§opa', '5op@', '§0pa', '$0p4', '50pa', '$op4', 'sopa', '$0p@', 's0p4', '5op4', '§op@', '5opa', '50p4', '§0p@', '50p@', 'sop4', '§0p4', '§op4', 's0p@', 'sop@'}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement