S
S
Spektar_X2021-12-19 16:21:20
Python
Spektar_X, 2021-12-19 16:21:20

How to find and remove a word from a string?

First, a natural number N is introduced — the number of useful tips.
Useful tips are introduced on the next N lines. It is guaranteed that the length of each is at
least 3 characters.

Output Format:
You should print the same tips in the same order, but those that start with the word
"not" (which can also be written as "Not") and a
space do not need to print this initial "not" and a space .

Example:
Input:

3
Мой руки перед едой!
Не суй пальцы в розетку!
Берегись автомобиля!
Conclusion:
Мой руки перед едой!
суй пальцы в розетку!
Берегись автомобиля!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Kirill Gusarev, 2021-12-19
@kaka888

Aloya228 's solution wo n't work here, as his code will remove all prefixes of non- and similar word fragments from the text.
We'll have to use regular expressions:

import re

# Из text будут удалены слова "не"
result = re.sub(r'\b[нН][еЕ]\b\s', '', text)
print(result)

In general, if you strictly follow the input format presented by you, then you can do this:
# Из text будет удалена подстрока "не ", если она находится в самом начале строки
result = re.sub('^[нН][еЕ] ', '', text)

Documentation for regular expressions: yy

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question