Current version

Aug 27, 2017 8:01 PM
@jam test You are reinventing the wheel, use [`str.translate`](https://docs.python.org/3/library/stdtypes.html#str.translate). To use `str.translate`, you need to first create a dictionary that can be used to map the original character to the new character. This transition table can be made using [`maketrans`](https://docs.python.org/3/library/stdtypes.html#str.maketrans).

>>> mapping = {'A': 'Q', 'B': 'W', 'C': 'E', 'D': 'R'}
>>> str.maketrans(mapping)
{65: 'Q', 66: 'W', 67: 'E', 68: 'R'}


Now that we have the table ready, we just need to pass it to `translate`.
 

Message history

Aug 27, 2017 8:01 PM
said: @jam test You are reinventing the wheel, use [`str.translate`](https://docs.python.org/3/library/stdtypes.html#str.translate). To use `str.translate`, you need to first create a dictionary that can be used to map the original character to the new character. This transition table can be made using [`maketrans`](https://docs.python.org/3/library/stdtypes.html#str.maketrans). >>> mapping = {'A': 'Q', 'B': 'W', 'C': 'E', 'D': 'R'} >>> str.maketrans(mapping) {65: 'Q', 66: 'W', 67: 'E', 68: 'R'} Now that we have the table ready, we just need to pass it to `translate`. >>> table = str.maketrans(mapping) >>> "AB".translate(table) 'QW' Now we get the desired result.