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.
이 연습은 강의의 일부입니다
Regular Expressions in Python
연습 안내
- Find if the substring
actoroccurs between the characters with index37and41inclusive. If it is not detected, print the statementWord not found. - Replace
actor actorwith the substringactorifactoroccurs only two repeated times. - Replace
actor actor actorwith the substringactorifactorappears three repeated times.
실습형 인터랙티브 연습
이 예제를 이 샘플 코드를 완성하여 풀어보세요.
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("____", "____"))