📜  slug - Python (1)

📅  最后修改于: 2023-12-03 14:47:29.300000             🧑  作者: Mango

Slug - Python

Slug is a package in Python that is used to create slugs from strings. A slug is a URL-friendly version of a string that typically includes only lowercase letters and hyphens.

Installation

You can install Slug with pip:

pip install slug
Usage

Import the slugify function from the slug package:

from slug import slugify

You can use this function to create a slug from a string:

text = "This is a string to be slugified"
slug = slugify(text)
print(slug)

This will output:

this-is-a-string-to-be-slugified
Features

Slug offers several features, including:

  • stripping HTML tags
  • converting non-URL-safe characters to their URL-safe counterparts
  • preserve non-ASCII characters

You can use these features by passing arguments to the slugify function:

text = "<h1>This is a string to be slugified with non-ASCII characters: åäö</h1>"
slug = slugify(text, allow_unicode=True, separator="_")
print(slug)

This will output:

this_is_a_string_to_be_slugified_with_non_ASCII_characters_åäö
Conclusion

In conclusion, Slug is a powerful tool for creating slugs from strings in Python. Its simple syntax and extensive feature set make it a great choice for developers looking to create URL-friendly strings.