Real Python Tutorials

How to Replace a String in Python
NEW

How to Replace a String in Python

In this tutorial, you'll learn how to remove or replace a string or substring. You'll go from the basic string method .replace() all the way up to a multi-layer regex pattern using the sub() function from Python's re module.

Jan 15, 2025 basics best-practices