Case Conversion Library
The `case-conversion` library for Python, currently at version 3.0.2, provides a robust, dependency-free solution for converting strings between various case styles such as camelCase, PascalCase, snake_case, kebab-case, and more. It offers automatic case detection, advanced acronym handling, and full Unicode support, making non-ASCII characters first-class citizens. The library is actively maintained and supports Python 3.10 and newer.
Warnings
- breaking The library explicitly requires Python 3.10 or higher. Users on older Python 3 versions (e.g., 3.9 or earlier) will encounter installation or runtime errors.
- gotcha While the library features automatic acronym detection, for complex or unusual sequences of capital letters (e.g., 'fooBADHTTPError'), the default behavior might not split them as desired. Custom acronyms can be provided during initialization to ensure correct parsing.
- gotcha The library offers 'auto-detection' of the input case, which is a powerful feature. However, for highly ambiguous or unconventional input strings, the detected word boundaries might not perfectly align with every user's specific interpretation. Always verify the output for edge cases.
Install
-
pip install case-conversion
Imports
- Converter
from case_conversion import Converter
- Top-level functions (e.g., camel, snake)
import case_conversion case_conversion.camel("some string")
Quickstart
from case_conversion import Converter
# Using the Converter class
converter = Converter()
camel_case_string = converter.camel("FOO_BAR_STRING")
pascal_case_string = converter.pascal("another string")
print(f"Camel Case: {camel_case_string}")
print(f"Pascal Case: {pascal_case_string}")
# Using top-level convenience functions
import case_conversion
snake_case_string = case_conversion.snake("Hello World Example")
kebab_case_string = case_conversion.kebab("http header case")
print(f"Snake Case: {snake_case_string}")
print(f"Kebab Case: {kebab_case_string}")
# Custom acronym handling
custom_acronym_converter = Converter(acronyms=["BAD", "HTTP"])
custom_acronym_snake = custom_acronym_converter.snake("fooBADHTTPError")
print(f"Custom Acronym Snake Case: {custom_acronym_snake}")
# Unicode support
unicode_snake = case_conversion.snake("fóó-bar-string")
print(f"Unicode Snake Case: {unicode_snake}")