Why the replace method doesn't work? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why the replace method doesn't work?

I'm trying to rename a string from "name.ttml.txt" to "name.srt.txt" the problem is that the replace method doesn't work. str = "name.ttml.txt" newstr = str.replace('.ttml', '.srt') print(str) print(newstr) The two outputs are the same, it appears that the method doesn't work.

10th Dec 2018, 6:27 AM
Emmanuele D'Ettorre
Emmanuele D'Ettorre - avatar
3 Answers
+ 4
The outputs were different when I ran the code in the Playground, and it worked as expected. Could you please save it in the Playground and share a link so we can see what's going on? Also, I'd recommend not using str as a variable name, as it's also a built-in function as well as a data type.
10th Dec 2018, 6:38 AM
Kishalaya Saha
Kishalaya Saha - avatar
+ 1
Oh I see, I was playing around on visual studio. Now I used an online interpreter and worked as expected. Can you suggest me an easier to use ide for python on windows?
10th Dec 2018, 7:08 AM
Emmanuele D'Ettorre
Emmanuele D'Ettorre - avatar
+ 1
That's strange! I just ran it on VS Code, and it gave me the expected output. What version of Python are you using? Could you please try it again after using a variable name other than str? Regarding Python IDE on Windows, I'm afraid I can't help you there. As you can see, I currently use VS Code myself. Before that I had used Emacs for a long time, which takes a while to get used to.
10th Dec 2018, 7:38 AM
Kishalaya Saha
Kishalaya Saha - avatar