127 – Remove punctuation functionally#
You already know how to remove punctuation from a string, but you can leverage functools.Placeholder (new in Python 3.14) and functools.partial to turn that into a function:
from functools import Placeholder as _P, partial
import string
remove_punctuation = partial(
str.translate,
_P,
str.maketrans("", "", string.punctuation),
)
Now, you can pass a string into the function remove_punctuation, which puts it as the first argument to str.translate because of the usage of Placeholder:
print(remove_punctuation("Hello, world!"))
# Hello world
This wouldn’t have been possible without Placeholder because str.translate only accepts positional-only arguments.
Further reading: