Accéder au contenu principal

Python String Replace Tutorial

Learn to find and replace strings using regular expressions in Python.
25 juin 2020  · 2 min de lecture

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.

Sujets

Learn more about Python

Cours

Introduction à Python

4 h
6.5M
Apprenez les bases de l’analyse de données avec Python en quatre heures et explorez ses principaux packages.
Afficher les détailsRight Arrow
Commencer le cours
Voir plusRight Arrow
Apparenté

Didacticiel

Python String format() Tutorial

Learn about string formatting in Python.
DataCamp Team's photo

DataCamp Team

Didacticiel

Python Regular Expression Tutorial

Discover the power of regular expressions with this tutorial. You will work with the re library, deal with pattern matching, learn about greedy and non-greedy matching, and much more!
Sejal Jaiswal's photo

Sejal Jaiswal

Didacticiel

Python Concatenate Strings Tutorial

Learn various methods to concatenate strings in Python, with examples to illustrate each technique.
DataCamp Team's photo

DataCamp Team

Didacticiel

String Split in Python Tutorial

Learn how you can perform various operations on string using built-in Python functions like split, join and regular expressions.
DataCamp Team's photo

DataCamp Team

Didacticiel

Using Regular Expressions to Clean Strings

This tutorial takes course material from DataCamp's Cleaning Data in Python course and allows you to clean strings using regular expressions.
Ryan Sheehy's photo

Ryan Sheehy

Didacticiel

Python String Tutorial

In this tutorial, you'll learn all about Python Strings: slicing and striding, manipulating and formatting them with the Formatter class, f-strings, templates and more!
Sejal Jaiswal's photo

Sejal Jaiswal

Voir plusVoir plus