Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.98 KB | None | 0 0
  1. # Ввод строки
  2. s = input()
  3.  
  4. # Ввод удаляемого слова
  5. sub = input()
  6.  
  7. # Знаки препинания
  8. chars = ".,?!;:-"
  9.  
  10. # Находим индекс удаляемого слова в строке
  11. pos = s.find(sub)
  12.  
  13. # Пока слово есть в строке
  14. while pos != -1:
  15.    
  16.     # Находим индекс символа после слова
  17.     char_index = pos + len(sub)
  18.    
  19.     # Если это знак препинания и сзади слова пробел - удаляем слово с пробелом
  20.     if (char_index < len(s)) and (s[char_index] in chars) and (pos != 0) and (s[pos - 1] == ' '):
  21.         s = s[:pos - 1] + s[char_index:]
  22.        
  23.     #Иначе просто удаляем слово
  24.     else:
  25.         s = s[:pos] + s[char_index:]
  26.        
  27.     # Обновляем индекс удаляемого слова в строке
  28.     pos = s.find(sub)
  29.  
  30. # Вывод результата
  31. print(s)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement