Python 3, replace in strings

python

I need to transliterate a Hebrew text. So wrote a little Python script, which reads-in the Hebrew text from a text file, globally replaces the Hebrew letters with Latin ones. Now I need to do some fine tuning (replace certain things in some position but not in other, so a global replace won't do). The trouble is the following: The read-in text is a string so I cannot replace individual string elements. So, what would be the best way to do it?

Best Answer

Convert your string into a list. Either a list of letters or (if this works in your case) a list of words. This is a basic approach but should work if your string is stored in writing order (rather than display order).

text_as_list = list(text_as_string)
replace_letters(text_as_list)  # this is the bit you're implementing
text_as_string = "".join(text_as_list)
Related Topic