Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Replace a String in Python (Summary)

In this video course, you’ve learned how to replace strings in Python. Along the way, you’ve gone from using the basic Python .replace() string method to using callbacks with re.sub() for absolute control. You’ve also explored some regex patterns and deconstructed them into a better architecture to manage a replacement script.

With all that knowledge, you’ve successfully cleaned a chat transcript, which is now ready for independent review. Not only that, but your transcript-sanitizing script has plenty of room to grow.

To continue your learning journey, check out:

Download

Sample Code (.zip)

961 bytes
Download

Course Slides (.pdf)

2.5 MB

00:00 Congratulations, you’ve made it to the last lesson of this video course, and you’ve covered quite some ground. You’ve learned how to replace strings in Python, and along the way, you’ve gone from using the basic Python .replace() string method to coming up with complex replacement rules with re.sub().

00:18 You’ve also explored some regex patterns with an online regex visualizer to prototype multiple replacement rules. With all that knowledge, you’ve successfully cleaned a chat transcript. Not only that, but your transcript-sanitizing script has plenty of room to grow.

00:37 What to do next? Now that you know how to replace strings in Python, here are a few suggestions. The transcript you’ve worked with in this video course contain timestamps.

00:47 If you want to learn how to create timestamps your own, then check out How to Get and Use the Current Time in Python. You’ll get your hands on a datetime object that represents the current time.

00:57 You’ll see how to format it according to international standards, and you’ll even check out how computers represent time.

01:05 If, after timestamps, your mind isn’t spinning fast enough, then how about diving deeper into the wonderful world of regular expressions? In this video course you’re currently watching, you were scratching the surface of what you can do with regular expressions.

01:19 To really get into the details, there’s a cool two-part series on regular expressions that you can read. In the first part, you’ll learn about another commonly used method of the re module: re.search().

01:31 You’ll also learn more about flags and a bit about the history of regex.

01:37 In the second part, you’ll learn when and how to pre-compile a regex in Python into a regex object. Along the way, you’ll also discover useful things that you can do with the match object that the .search() function returns. And that’s a wrap.

01:53 Are there any real-life examples where replacing strings came in handy for you? Let the Real Python community know in the comments below. I hope you had a huffing emoji.

02:02 Thanks for watching, and see you next time on realpython.com.

Become a Member to join the conversation.