Loading video player…

Replace a String in Python (Overview)

If you’re looking for ways to remove or replace all or part of a string in Python, then this course is for you. You’ll be taking a fictional chat room transcript and sanitizing it using both the .replace() method and the re.sub() function.

In Python, the .replace() method and the re.sub() function are often used to clean up text by removing strings or substrings or replacing them. In this tutorial, you’ll be playing the role of a developer for a company that provides technical support through a one-to-one text chat. You’re tasked with creating a script that’ll sanitize the chat, removing any personal data and replacing any swear words with emoji.

You’re only given one very short chat transcript:

[support_tom] 2022-08-24T10:02:23+00:00 : What can I help you with?
[johndoe] 2022-08-24T10:03:15+00:00 : I CAN'T CONNECT TO MY BLASTED ACCOUNT
[support_tom] 2022-08-24T10:03:30+00:00 : Are you sure it's not your caps lock?
[johndoe] 2022-08-24T10:04:03+00:00 : Blast! You're right!

Even though this transcript is short, it’s typical of the type of chats that agents have all the time. It has user identifiers, ISO time stamps, and messages.

In this case, the client johndoe filed a complaint, and company policy is to sanitize and simplify the transcript, then pass it on for independent evaluation. Ultimately, you want the transcript to look like this:

Agent  : What can I help you with?
Agent  : Are you sure it's not your caps lock?
Client : 😤! You're right!

Sanitizing the message is your job, and that’s what you’ll tackle in this video course.


Sample Code (.zip)

961 bytes

Course Slides (.pdf)

2.5 MB

00:00 Welcome to Replacing a String in Python. I’m Philipp with Real Python, and today I’ve got an important task for you. Imagine yourself as a developer working for a company that offers technical support through one-on-one text chats.

00:14 Your mission is to create a script that sanitizes the chat by removing any personal data and replacing any inappropriate language with fun emojis.

00:26 The chat transcript looks like this.

00:28 Every line is a message from either support_tom or johndoe with a timestamp that looks very funky right now and their messages.

00:38 You will see the transcript in detail in the next lesson, and after a hard day of work—and by that I mean after following this video course—the chat transcript must look like this. So compared to the one before, it’s much, much cleaner. And again, we will go into details in one of the next lessons.

00:55 And in case you’re using a screen reader, you will find the before chat transcript and the after chat transcript in the description below this video. So, how would you tackle this task?

01:07 Of course, you could go over it by hand, but where is the fun in that? Aren’t we all learning to program to lean back and let Python do a job like this? Of course we do. And in this video course, you will learn how you’ll gain some powerful knowledge on remove or replacing parts of a string or even the whole string With Python. Specifically, you’ll learn how to remove or replace a string or substring with the .replace() method. You’ll create regular expressions—you’ll learn about them in a moment—and leverage re.sub() to make complex rules and set up multiple replacement rules. Sounds good? Okay, let’s get started.

Become a Member to join the conversation.