Python String Replace

Learn to find and replace strings using regular expressions in Python.

In order to keep working with your prediction project, your next task is to figure out how to handle negations that occur in your dataset. Some algorithms for prediction do not work well with negations, so a good way to handle this is to remove either not or n't, and to replace the next word by its antonym.

Let's imagine that you have the string: The movie isn't good. You will need to remove n't and replace good for bad. This way, your string ends up being The movie is bad. You notice that in the first column of the dataset, you have a string that uses the word isn't followed by important.

The text of this column has been already saved in the variable movies so you start working with it. You can use print(movies) to view it in the IPython Shell.


TRY IT YOURSELF: Access the exercise in our Regular Expressions in Python course here.

  • Replace the substring isn't for the word is.
  • Replace the substring important for the word insignificant.
  • Print out the result contained in the variable movies_antonym.
# Replace negations
movies_no_negation = movies.____("____", "____")

# Replace important
movies_antonym = movies_no_negation.____("____", "____")

# Print out


To learn more about regular expressions, 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.