'Making a re.sub using a list of punctuation

Is there a way to take a list of punctuation, and put it into a re.sub. I was using f-string literals but the escape character breaks everything. Should I just type it manually?

marks = [',', '。', '—', '《', '》', '□', '●', '/', '{', '}', '·', '、', '「', '」','|']
punctuation = '|'.join(['\\' + f'{n}' for n in marks])
re.sub('\ |\?|\.|\!|\/|\;|\:', '', 'line')


Solution 1:[1]

Nothing wrong with what you're doing, though I prefer to use a character class. Can't be 100% sure from your post but it sounds like you're trying to remove certain punctuation from a string? Example:

marks = [',', '?', '—', '?', '?', '?', '?', '/', '{', '}', '·', '?', '?', '?','|']
punctuation = '[' + ''.join(['\\' + f'{n}' for n in marks]) + ']'

x = re.sub(punctuation, '', 'li..n?e-asdfa?sdf??ag\/as?dga,s|g`da')

print(punctuation)
print(x)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ejkeep