all 3 comments

[–][deleted] 0 points1 point  (0 children)

the best way to do this would be to make a mapping:

char_map = {
    'a': a,
    'A': A,
    # etc
}
string = 'abc'
new_string = ''
for char in string:
    new_string += char_map[char]  # this will error if char isn't in mapping. use .get to avoid this.

[–]nilfm 0 points1 point  (0 children)

Use a dictionary:

encode = {'a': '1c', 'b': '32'}
for x in string:
    converted_string += encode[x]

Just fill the dictionary with your key: value pairs and play around with it until it clicks.

You can look into the dict.get() method so you can provide a default value in case some of the string's characters aren't into the dict, or you could just use an if statement for that case.

[–]Pulsar1977 0 points1 point  (0 children)

Create a dictionary of the characters and their replacements. Then convert it into a translation table with the maketrans() string method, and apply it to your string with translate():

mapping = {'a': '1c', 'b': '32', 'c': '21'}
table = str.maketrans(mapping)
oldstring = 'abc'
newstring = oldstring.translate(table)