Reassign problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Reassign problem

Is it possible to reassign a letter in a string? Like: str='work' I want to replace k with d, but str='work' str[3]=d is not working. Is there any other useful way?

5th Feb 2017, 9:22 AM
C.R.Devila
C.R.Devila - avatar
2 Answers
+ 3
strings are not mutable (i.e. cannot be changed) in Python. One approach could be: turn the string into a list, then change the list, and finally use 'join' to turn the list back into a string.
5th Feb 2017, 9:38 AM
Álvaro
+ 1
you can use somestring=somestring.replace (oldchar, newchar)
5th Feb 2017, 12:05 PM
Geo Tab