Inflector for Python
Inflector is a Python library (version 3.1.1) designed for transforming strings, primarily focusing on pluralizing and singularizing English and Spanish nouns. It also provides utilities for converting strings between various cases like CamelCase, snake_case, and human-readable formats. It serves as a port of the Ruby on Rails Inflector, with Spanish language support developed independently. The project sees infrequent but notable updates, with the latest release in January 2024.
Warnings
- gotcha Users often confuse 'inflector' with other similar Python libraries like 'inflection' or 'inflect'. While they share similar goals, their APIs are different. 'inflector' requires class instantiation, whereas 'inflection' provides direct module-level functions, and 'inflect' uses an engine class.
- gotcha The `inflector` library explicitly requires you to instantiate the `Inflector` class before using its methods (e.g., `inflector_instance.pluralize('word')`). Attempting to call methods directly on the module (e.g., `inflector.pluralize('word')`) will result in an `AttributeError`.
- gotcha This library explicitly states that it supports pluralization and singularization only for English and Spanish nouns. It does not provide comprehensive support for other languages.
Install
-
pip install inflector
Imports
- Inflector
from inflector import Inflector
Quickstart
from inflector import Inflector
inflector = Inflector()
# Pluralization
print(f"Plural of 'cat': {inflector.pluralize('cat')}")
print(f"Plural of 'person': {inflector.pluralize('person')}")
# Singularization
print(f"Singular of 'cats': {inflector.singularize('cats')}")
print(f"Singular of 'people': {inflector.singularize('people')}")
# CamelCase to underscore
print(f"Underscore 'CamelCase': {inflector.underscore('CamelCase')}")
# Underscore to CamelCase
print(f"Camelize 'device_type': {inflector.camelize('device_type')}")
print(f"Camelize 'device_type' (lower first): {inflector.camelize('device_type', False)}")