Chain the .replace() Method
Here you have the chat transcript again. I already assigned the content of the chat transcript to a variable named
transcript. To make the code more readable, you can use triple-quote syntax (
""") to define a multiline string.
Also, note that you put a backslash (
\) after your triple quotes. Without the backslash, you would have a new line at the beginning of your transcript. With the backslash, you can put the transcript nicely looking into three lines after the triple quotes, and put the backslash after the triple quotes to say, like, Hey, Python, I know there is a new line, but please remove it. Now, you can use the
.replace() method to replace the uppercase word
"BLASTED" with a more polite word or a huffing emoji. When you press Enter, then you can see the output in the shell.
but there is still another occurrence of
"blast" in the last message from John Doe. That’s not surprising because the
.replace() method only replaces the exact string you provide as the first argument.
This means that if you are using the
.replace() method, you’ll need to call it various times with the variations. In this case, you can just chain on another call to
.replace(). So let’s do it again.
When you press Enter, then you can see that the output is now exactly what you want. Both the uppercase
"BLASTED" and the word
"Blast" are replaced with the huffing emoji. But keep in mind, since you didn’t assign the output from
.replace() to a new variable, the original transcript still stays the same. And yeah, now we replaced this swear word, but there are other things we need to sanitize.
So we would have to place another
.replace() on another
.replace(), and so on. So you might be thinking that there is a better way to handle this, especially for a general-purpose transcription sanitizer like the one you want to create for your work at hand, right?
Become a Member to join the conversation.