Library reference
Example page
The project, data and links on this page are made up, to show what dokk’s components can do. It does not document dokk itself.
slugkit turns text into URL slugs. This is its API reference.
pip install slugkit
uv add slugkit
poetry add slugkit
from slugkit import slugify
slugify("Héllo, Wörld!") # "hello-world"
slugify #
def slugify(text: str, *, separator: str = "-", max_length: int | None = None,
lowercase: bool = True, stopwords: Iterable[str] = ()) -> str
Turn any text into a slug: accents are removed, everything that is not a letter or digit becomes the separator, and repeated separators collapse into one.
| Parameter | Type | Default | Description |
|---|---|---|---|
text |
str |
required | The text to convert. |
separator |
str |
"-" |
Put between words. |
max_length |
int | None |
None |
Cut the slug at a word boundary, at most this long. |
lowercase |
bool |
True |
Lowercase the result. |
stopwords |
Iterable[str] |
() |
Words to leave out, such as {"a", "the"}. |
Returns the slug, which may be empty. Raises TypeError when text is not a string.
>>> slugify("The Quick Brown Fox", stopwords={"the"})
'quick-brown-fox'
>>> slugify("A very long title indeed", max_length=12)
'a-very-long'
unique_slug #
def unique_slug(text: str, taken: Container[str], **options) -> str
Like slugify, but adds -2, -3 and so on until the slug is not in taken.
New in 2.1
taken = {"hello", "hello-2"}
unique_slug("Hello", taken) # "hello-3"
Slugifier #
class Slugifier(separator: str = "-", **options)
Keep a set of options to reuse; calling the object slugifies.
slug = Slugifier(separator="_", max_length=40)
slug("Hello World") # "hello_world"
Exceptions #
SlugError- Base class for slugkit’s own errors.
EmptySlugError- Raised by
slugify(..., allow_empty=False)when nothing is left.
Deprecated #
Removed in 3.0
slugkit.make_slug() still works but warns. Use slugify: it takes the same
arguments, except sep, now called separator.