A more flexible solution for when you have to make many changes to string
is to save it as a list using list
:
texto = list("o seu texto aqui")
This causes you to display your content as it appears in a normal list:
>>> texto
['o', ' ', 's', 'e', 'u', ' ', 't', 'e', 'x', 't', 'o', ' ', 'a', 'q', 'u', 'i']
In this way you can change each of the positions freely as you were doing:
>>> texto[2] = chr(ord(texto[2]) + 1)
>>> texto
['o', ' ', 't', 'e', 'u', ' ', 't', 'e', 'x', 't', 'o', ' ', 'a', 'q', 'u', 'i']
Note that you can not simply add a caratere with a number. You must first fetch the ascii value of the letter with the ord
, add the value you want, and convert back to caratere with chr
.
And whenever you want to show again as a normal string, just use the join
>>> ''.join(texto)
'o teu texto aqui'