2

I'm trying to replace a string that contains letters from the English keyboard "gjceljvjtxyfz" to the corresponding Russian "посудомоечная". I am NOT trying to translate or transliterate from English to Russian, but to replace the English letters with the corresponding letters on the Russian keyboard. I haven't found any Python libraries or useful code to solve this problem. Any help would be appreciated.

  • What you seek is called **transliteration** and you can check this similar question already asked here: https://stackoverflow.com/q/14173421/4636715 – vahdet Jul 13 '20 at 07:47
  • @vahdet no that's not what I'm seeking. I want to replace the letters in the English keyboard with the ones in Russian. For instance: "qwerty" should be replaced with "йцукен" – Vadim Katsemba Jul 13 '20 at 07:55
  • @vahdet There is no way that this is transliteration in the ordinary sense. That Russian word could be transliterated something like posudomoyechnaya. It shouldn't be a surprise that there isn't a word Russian pronounced gjceljvjtxyfz... – alani Jul 13 '20 at 07:55
  • Ah ok, sorry for that. Yet, I am not deleting in order not to make the mentioning comments obsolete. – vahdet Jul 13 '20 at 08:05

1 Answers1

4

You can use the translate method. It requires a mapping of integers (character codes) to characters (strings of length 1). You can create this mapping manually, like so:

translation = {}
translation[ord('q')] = 'й'
translation[ord('w')] = 'ц'
...

and then use it:

s1 = "gjceljvjtxyfz"
s2 = s1.translate(translation)

If you want to avoid long and inefficient code which creates the translation table, you can use maketrans:

translation = str.maketrans(dict(zip('qwerty','йцукен')))

The specifics are slightly different for Python 2 and 3; the code sections above are for Python 3.

anatolyg
  • 26,506
  • 9
  • 60
  • 134