Get startedGet started for free

Finding a substring

It's a new day at work and you need to continue cleaning your dataset for the movie prediction project. While exploring the dataset, you notice a strange pattern: there are some repeated, consecutive words occurring between the character at position 37 and the character at position 41. You decide to write a function to find out which movie reviews show this peculiarity, remembering that the ending position you specify is not inclusive. If you detect the word, you also want to change the string by replacing it with only one instance of the word.

Complete the if-else statement following the instructions.

The text of three movie reviews has been already saved in the variable movies. You can use print(movies) to view the variable in the IPython Shell.

This exercise is part of the course

Regular Expressions in Python

View Course

Exercise instructions

  • Find if the substring actor occurs between the characters with index 37 and 41 inclusive. If it is not detected, print the statement Word not found.
  • Replace actor actor with the substring actor if actor occurs only two repeated times.
  • Replace actor actor actor with the substring actor if actor appears three repeated times.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

for movie in movies:
  	# If actor is not found between character 37 and 41 inclusive
    # Print word not found
    if movie.find("____", ____, ____) == ____:
        print("Word not found")
    # Count occurrences and replace two with one
    elif movie.____("____") == 2:  
        print(movie.replace("____", "____"))
    else:
        # Replace three occurrences with one
        print(movie.replace("____", "____"))
Edit and Run Code