Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
Does .replace() always need to be declared inside a print() statement or can you use it to change a string permanently? If so, how?
.replace()
print()
Since strings are "immutable" objects, you can't mutate it at all. It's true for all of it's methods.
.replace is also create a new string object for you.
.replace
s1 = 'hello' s2 = s1.replace('e', '3') print(s1, id(s1)) print(s2, id(s2))
Add a comment
You don't need to use .replace() always inside of print()
Like the below example, you can change the str with .replace() and then print changed str
str
myStr = "I love programming" myStr = myStr.replace("programming", "chicken Nugget") print(myStr)
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.