Вот несколько методов, которые можно использовать для удаления знаков препинания из строки в Python:
Метод 1: использование string.punctuation
Метод 2. Использование регулярных выражений
import re
def remove_punctuation(text):
return re.sub(r'[^\w\s]', '', text)
Метод 3. Использование цикла
def remove_punctuation(text):
punctuations = '''!()-[]{};:'"\,<>./?@#$%^&*_~'''
for punctuation in punctuations:
text = text.replace(punctuation, '')
return text
Метод 5. Использование библиотеки unicodedata
import unicodedata
def remove_punctuation(text):
return ''.join(
char for char in text if not unicodedata.category(char).startswith('P')
)