A more flexible solution for when you have to make many changes in string
is to store it as a list using list
:
texto = list("o seu texto aqui")
This makes when you show your content, it appears as a normal list:
>>> texto
['o', ' ', 's', 'e', 'u', ' ', 't', 'e', 'x', 't', 'o', ' ', 'a', 'q', 'u', 'i']
In this way it will be possible to change each position freely, as it was 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 cannot simply add a character to a number. First you have to fetch the value ascii of the letter with the function ord
, add to the value you want, and convert back to character with chr
.
And whenever you want to show again as a normal string, just use the Join method
>>> ''.join(texto)
'o teu texto aqui'
Really this way is much more interesting to do. Thanks!
– Antony Leme