A small Python library for checking whether a character is a punctuation character.
Note
Our ispunct function should return true wherever C's ispunct function returns true. This is ensured by checking string.punctuation. However, we extend this beyond the cases handled in the C function by checking the input character's category code.
from ispunct import ispunct
assert ispunct("?")
assert not ispunct("a")
assert ispunct("‽")This package is published on PyPI. You can install it with PIP:
$ pip add ispunct
Or, if using UV for dependency management:
$ uv add ispunct
The origin of this project comes from an equivalent function to Julia's ispunct, which is itself derived from the C implementation for obtaining a Unicode character's category code. This is a more complete solution than checking against string.punctuation or curses.ascii.ispunct. There is also a StackOverflow question for this functionality which I have answered.
This library also implements (and uses internally) bitwise functions to calculate the number of leading/trailing zeros/ones in the bitwise representation of a Python integer. We also compute a Python integer that has the same bitpattern as a given character (i.e., simulating Julia's bitcast). These are required in order to determine a character's category code.
This was written mostly as a proof of concept using bitwise operations on character codes. You could derive similar functionality from the standard library unicodedata, or matching on character category with regex (see examples here).
A spiritual sister package and successor to ispunct is strip-marks, in which we further explore unicode operations in Python.
If your research depends on ispunct, please consider giving us a formal citation: citation.bib.