how to re assign an index's string value in Python | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

how to re assign an index's string value in Python

index string

30th Apr 2019, 4:10 PM
Andrew
Andrew - avatar
2 Answers
+ 4
Strings are immutable. s = 'Hello', s[1] = 'a' will result in an error (if that's what you're asking). You can implement a function to alter strings though: def alter_string(s, index, new): return s[:index] + new + s[index+1:] s = 'Hello' print(alter_string(s, 1, 'a')) # Hallo
30th Apr 2019, 4:45 PM
Anna
Anna - avatar
0
Do you have an example of what you want? Just add it to the question
30th Apr 2019, 4:12 PM
Trigger
Trigger - avatar