Skip to content

Repository files navigation

ispunct

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.


Quick Start

from ispunct import ispunct

assert ispunct("?")
assert not ispunct("a")
assert ispunct("‽")

Using ispunct as a Library

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

History

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.

Notes on Internal Functionality

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.

Alternative Libraries

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.

Citation

If your research depends on ispunct, please consider giving us a formal citation: citation.bib.

About

A small Python library to check if a character is a punctionation character

Resources

Stars

Watchers

Forks

Releases

Packages

Used by

Contributors

Languages