Why string isn't changed ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why string isn't changed ?

soz = "Python" soz.replace("P" , "7" ) print(soz)

28th May 2020, 12:16 AM
Gökhan TUNCER
Gökhan TUNCER - avatar
3 Answers
+ 3
You need to understand that string are NOT mutable : contrary to lists, for instance, you cannot change its content. All methods that "change" a string actually return a new string with changes applied (the old one isn't modified). So when working with strings, always store the result!
28th May 2020, 5:59 AM
Théophile
Théophile - avatar
+ 6
You need to store or use the return value of replace-method. It does not apply it's return value automatically on the object it was executed with.
28th May 2020, 12:20 AM
Sandra Meyer
Sandra Meyer - avatar
+ 1
Théophile , Perfect Answer to the question.
28th May 2020, 8:11 AM
$¢𝐎₹𝔭!𝐨𝓝
$¢𝐎₹𝔭!𝐨𝓝 - avatar