From the course: Python Practice: Operations

Unlock this course with a free trial

Join today to access over 25,500 courses taught by industry experts.

Solution: String replacement

Solution: String replacement - Python Tutorial

From the course: Python Practice: Operations

Solution: String replacement

- [Instructor] To solve this challenge, we'll need to have our function accept three pieces of information. We'll need the original string we're working with, we'll need the value we want to replace, and we'll need the new value that replaces that old value. So I added three parameters here in my function definition; the original string, the old string, and the new string. Then I used the string method replace on the original string, and I give it the old string and the new string values. Replace will use this information to create a new string where any occurrence of the value of the old string is replaced by the value of the new string. This new string is what I return from the function. I'll test my code and I see that I get the correct result. String replacement comes up a lot when we're writing programs that deliver output, either to users or to other systems and processes.

Contents