- Read Tutorial
- Watch Guide Video
In Python, it's not even possible. So thankfully there are a number of workarounds and in this guide, I want to talk about the Replace function and it is not possible to leverage Replace in order to completely replace a string. So what we're going to do is leverage the ability to replace and then reassign a string value.
Now if that entire process sounds about as clear as mud. Don't worry we're going to work through an example. So yes a sentence and we'll go with our favorite one. The quick brown fox jumped over the lazy dog
sentence = 'The quick brown fox jumped over the lazy dog'
and now let's talk about how we can perform reassignment. So reassignment is simply just like it sounds, it gives us the ability to reassign a value. So if we wanted to say write over the sentence value I could say "sentence" and then say new value. If I come down here and print out the sentence you may guess what is going to be printed out. Right here we get new value.
Now we did not change this string. Remember when we did that very slow break out where I showed that a string is not the variable? That these are different elements, that we have a variable we have an assignment operator then we have the string itself and so what we're doing here is when we are redefining this or when we're reassigning a new value to be in the sentence. We are not touching the string. All we're telling the Python interpreter is that the sentence variable now is equal to this string. But these are completely different strings and when this new value got printed out it is pointing to this exact string where we assigned it to be 'New value'.
So with this knowledge what we can do is not worry about changing the string because that's not possible in python but what we can do is we can take that string, perform any changes that we want on it, and then reassign it and we can even reassign it back into the sentence variable. So what I'm going to do here is I am going to call sentence and then here I'm going to say sentence.replace
Replace takes two arguments it takes the word that you're looking for and then it takes the word you want to replace it with. So here I'm going to say I want you to try to find quick and I want to replace it with slow. So now if I return you can see that it says the slow brown fox jumped over the lazy dog.
So what happened here? We took the sentence right here which was assigned to this specific string and then we reassigned it into the same name and this is the same variable but we took the reference to that string so we took that sentence and we said I want you to replace quick with slow and so that is the process of being able to not change a string because that's not possible. But to leverage the replace keyword the replace function with a reassignment process and so we're able to accomplish the exact same type of goal we had which was to take one string and to change it but we were able to do it within the rules that Python gives us.
That's how you can leverage the replace function inside of Python.
Code
sentence = 'The quick brown fox jumped over the lazy dog.' sentence = sentence.replace('quick', 'slow') print(sentence.replace('quick', 'slow')) print(sentence)