Skip to main content
HomeAbout PythonLearn Python

Python String Replace Tutorial

Learn to find and replace strings using regular expressions in Python.
Jun 2020  · 2 min read

Sometimes you will want to replace occurrences of a substring with a new string, and Python has a built-in method .replace() to achieve it.

Python provides you with the .replace() string method that returns a copy of the string with all the occurrences of old substring replaced with the new substring given as a parameter.

string replace

Arguments

It takes three arguments: the substring to replace, the new string to replace it, and an optional number that indicates how many occurrences to replace.

  • old − This is the old substring to be replaced.

  • new − This is the new substring, which would replace the old substring.

  • count − If this optional argument count is given, only the first count occurrences are replaced.

Example of String Replace

In the example code below, you will replace the substring house with car. The method will return a copy with all the house substrings replaced.

example 1

Example of String Replace with the Count Optional Argument

In this second example, you specify that you only want two of the occurrences to be replaced. In the below output, you can see that the method returns the string's copy with the first two occurrences of house replaced by car.

example 2

Example

Here you will replace the substring isn't for the word is. Then, replace the substring important for the word insignificant, and finally, print out the result contained in the variable movies_antonym.

replace substring

When we run the above code, it produces the following result:

the rest of the story is insignificant because all it does is serve as a mere backdrop for the two stars to share the screen .

Try it for yourself.

To learn more about Python String Replace, please see this video from our course, Regular Expressions in Python.

This content is taken from DataCamp’s Regular Expressions in Python course by Maria Eugenia Inzaugarat.

Check out our Python String Tutorial.

Topics

Learn more about Python

Course

Regular Expressions in Python

4 hr
37.3K
Learn about string manipulation and become a master at using regular expressions.
See DetailsRight Arrow
Start Course
Certification available

Course

Introduction to Python

4 hr
5.4M
Master the basics of data analysis with Python in just four hours. This online course will introduce the Python interface and explore popular packages.
See MoreRight Arrow
Related

Mastering the Pandas .explode() Method: A Comprehensive Guide

Learn all you need to know about the pandas .explode() method, covering single and multiple columns, handling nested data, and common pitfalls with practical Python code examples.
Adel Nehme's photo

Adel Nehme

5 min

Python NaN: 4 Ways to Check for Missing Values in Python

Explore 4 ways to detect NaN values in Python, using NumPy and Pandas. Learn key differences between NaN and None to clean and analyze data efficiently.
Adel Nehme's photo

Adel Nehme

5 min

Seaborn Heatmaps: A Guide to Data Visualization

Learn how to create eye-catching Seaborn heatmaps
Joleen Bothma's photo

Joleen Bothma

9 min

Test-Driven Development in Python: A Beginner's Guide

Dive into test-driven development (TDD) with our comprehensive Python tutorial. Learn how to write robust tests before coding with practical examples.
Amina Edmunds's photo

Amina Edmunds

7 min

Exponents in Python: A Comprehensive Guide for Beginners

Master exponents in Python using various methods, from built-in functions to powerful libraries like NumPy, and leverage them in real-world scenarios to gain a deeper understanding.
Satyam Tripathi's photo

Satyam Tripathi

9 min

Python Linked Lists: Tutorial With Examples

Learn everything you need to know about linked lists: when to use them, their types, and implementation in Python.
Natassha Selvaraj's photo

Natassha Selvaraj

9 min

See MoreSee More