Стоит отметить, что python имеет полосы, полоски и полоски. lstrip и rstrip удаляют символы для начала и конца строки.
Используя аналогичный код, который у вас есть.
sentance="?hello !mango! ...and., ban,ana.. yum?? apple!"
punctuations="!?.,"
def beautify_sentence(sentence, punctuation):
words = []
for word in sentance.split(' '):
words.append(word.lstrip(punctuations).rstrip(punctuations))
return " ".join(words)
print(beautify_sentence(sentance, punctuations))
Но, как уже упоминалось, полоса будет удалена спереди и сзади.
sentance="?hello !mango! ...and., ban,ana.. yum?? apple!"
punctuations="!?.,"
def beautify_sentence(sentence, punctuation):
words = []
for word in sentance.split(' '):
words.append(word.strip(punctuations))
return " ".join(words)
print(beautify_sentence(sentance, punctuations))