Python-Slugify: Create SEO-Friendly Unicode Slugs

Python-Slugify: Your Go-To Tool for Clean, SEO-Friendly Slugs

In the realm of web development and data management, converting human-readable text into URL-friendly strings—known as 'slugs'—is a common necessity. Whether it's for blog post URLs, image filenames, or unique identifiers in a database, slugs play a crucial role in usability and search engine optimization (SEO).

Enter python-slugify, a powerful and highly configurable Python library designed to handle this very task with exceptional grace, especially when dealing with the complexities of Unicode characters.

What is a Slug?

A slug is a part of a URL that identifies a particular page in a human-readable keyword format. For example, in yourwebsite.com/blog/my-awesome-post-title, my-awesome-post-title is the slug.

Why python-slugify?

This library stands out for its robust handling of Unicode strings, a feature often overlooked in simpler slugification tools. It intelligently converts characters from various languages into their ASCII equivalents, ensuring your slugs are universally compatible and meaningful.

Key Features:

  • Unicode Support: Seamlessly converts international characters (e.g., 'C\'est déjà l\'été.' becomes 'c-est-deja-l-ete'; '影師嗎' becomes 'ying-shi-ma').
  • Customizable Options: Offers a wide array of parameters to control the output, including:
    • entities, decimal, hexadecimal: Handle HTML entities.
    • max_length, word_boundary, save_order: Control slug length and word integrity.
    • separator: Define the character used to separate words (default is hyphen -).
    • stopwords: Remove common words like 'the', 'in', 'a' to keep slugs concise.
    • regex_pattern: Apply custom regular expressions for advanced character filtering.
    • lowercase: Option to preserve original casing.
    • replacements: Define custom character or string replacements (e.g., [['|', 'or']]).
    • allow_unicode: Retain original unicode characters if desired.
  • Easy Installation: Available via pip, making integration into your Python projects straightforward.

    pip install python-slugify
    # Or, with an alternative unidecode package:
    pip install python-slugify[unidecode]
    
  • Command Line Tool: Comes with a convenient slugify command-line utility for quick transformations directly from your terminal.

    echo "Taking input from STDIN" | slugify --stdin
    # Output: taking-input-from-stdin
    
    slugify taking input from the command line
    # Output: taking-input-from-the-command-line
    

How to Use:

Using python-slugify is intuitive. Simply import the slugify function and pass your text:

from slugify import slugify

# Basic usage
txt = "This is a test ---"
r = slugify(txt)
print(r) # Output: this-is-a-test

# Handling non-English characters
txt = 'Компьютер'
r = slugify(txt)
print(r) # Output: kompiuter

# With max_length and word_boundary
txt = 'jaja---lol-méméméoo--a'
r = slugify(txt, max_length=15, word_boundary=True)
print(r) # Output: jaja-lol-a

# Removing stopwords
txt = 'the quick brown fox jumps over the lazy dog'
r = slugify(txt, stopwords=['the'])
print(r) # Output: quick-brown-fox-jumps-over-lazy-dog

# Custom replacements
txt = '10 | 20 %'
r = slugify(txt, replacements=[['|', 'or'], ['%', 'percent']])
print(r) # Output: 10-or-20-percent

License and Dependencies

python-slugify is released under the MIT License, making it free for commercial and personal use. By default, it uses text-unidecode for decoding, which is GPL & Perl Artistic licensed. For those who prefer, an alternative Unidecode (GPL) package can be installed.

Contribution

This project welcomes contributions. Developers are encouraged to read the contribution guidelines before submitting pull requests.

With over 1.5k stars and 110 forks on GitHub, python-slugify is a well-maintained and widely used tool that simplifies a crucial aspect of web content management. Its flexibility and comprehensive feature set make it an invaluable addition to any Python developer's toolkit.

Original Article: View Original

Share this article