Python. Замена слов в большом тексте

Пользователь вводит текст (большой текст). В тексте буду встречаться слова, которые нужно заменить "решеткой". Но если будет слово чертежник то выходит, ####ежник...

Как это работает? пробелы не работают без модуля import re

text = input("anything: ")
text = text.lower()
for x, y in ("Черт", "####"):
    text = text.replace(x, y)
print(text)

Ответы (1 шт):

Автор решения: Maksi_mak2
text = input("anything: ")
text = text.lower() # ловер - а сами написали Черт 
cen = {"черт":"####"}
for key, value in cen.items():
    text = text.replace(key, value)
print(text)

Но такой заменит и чертежника

text = input("anything: ")
text = text.lower() 
text_split = text.split()
cen = {"черт":"####"}
for key, value in cen.items():
    for i in range(len(text_split)):
        if text_split[i] == key: 
            text_split[i] = value

а такой уже нет

Ответ:

def list_to_str(list_ob):
    ret = ""
    for i in list_ob:
        ret+=f"{i} "
    ret = ret.replace("\\n", "\n")
    return ret


text = """
черт
мой чертежник"""
text = text.lower() 
text_split = text.replace("\n", "\\n").split()
cen = {"черт":"####"}
for key, value in cen.items():
    for i in range(len(text_split)):
        if text_split[i] == key: 
            text_split[i] = value
print(list_to_str(text_split))

Однако, такой не уберет "черт,", "чёрт!" и тд., а только "черт"

→ Ссылка