textcase
textcase is a lightweight, performant Python library designed for comprehensive text case conversions. It supports a wide array of cases like snake_case, kebab-case, and camelCase, and handles complex word boundaries including acronyms and non-ASCII characters. The library is notable for having zero external dependencies and a strong focus on accuracy and extensibility. It is actively maintained with frequent releases, with the current version 0.4.5, released in October 2025, ensuring Python 3.14 compatibility.
Common errors
-
ModuleNotFoundError: No module named 'textcase'
cause The 'textcase' library is not installed in the current Python environment.fixRun `pip install textcase` to install the library. -
AttributeError: module 'textcase' has no attribute 'convert'
cause You are attempting to use the `convert` function, which was part of an older API (pre-0.4.0 rewrite). The current API uses direct methods on the `textcase` module.fixUpdate your code to use the new API. For example, replace `convert(my_string, case.SNAKE)` with `textcase.snake(my_string)`. -
TypeError: 'Case' object is not callable
cause This error can occur if you're trying to call `textcase.case.SNAKE()` as a function, or if you're attempting to access `case` or `convert` from an outdated import pattern after the 0.4.0 rewrite.fixEnsure you are using `import textcase` and calling methods directly on the module, e.g., `textcase.snake("my string")`. The `case` object is no longer directly exposed in the same way for conversion functions.
Warnings
- breaking Version 0.4.0 introduced a significant rewrite of the library's internal structure and API. The primary way to import and use the library changed from `from textcase import case, convert` to directly importing `textcase` and calling methods on the module (e.g., `textcase.snake()`).
- gotcha The library primarily focuses on case conversion based on defined word boundaries. For languages or specific contexts requiring Unicode normalization (e.g., decomposing accented characters before processing), `textcase` does not perform this automatically. Users should handle Unicode normalization separately if needed.
- gotcha Behavior around stripping leading/trailing punctuation and handling specific delimiters was refined in versions 0.4.2 and 0.4.4. If you rely on very specific edge-case punctuation handling, test thoroughly after updating, as outputs might have subtly changed due to bug fixes and optimizations.
Install
-
pip install textcase
Imports
- textcase
from textcase import case, convert
import textcase
Quickstart
import textcase
input_string = "Hello, beautiful_World! How are you in this wonderful-Day?"
# Convert to snake_case
snake_case_string = textcase.snake(input_string)
print(f"Snake Case: {snake_case_string}")
# Convert to camelCase
camel_case_string = textcase.camel(input_string)
print(f"Camel Case: {camel_case_string}")
# Convert to KEBAB-CASE
kebab_case_string = textcase.kebab(input_string)
print(f"Kebab Case: {kebab_case_string}")
# Convert to PascalCase
pascal_case_string = textcase.pascal(input_string)
print(f"Pascal Case: {pascal_case_string}")
# Check if a string matches a specific case
is_kebab = textcase.kebab.match("my-css-class")
print(f"'my-css-class' is Kebab Case: {is_kebab}")
is_snake = textcase.snake.match("my_css_class")
print(f"'my_css_class' is Snake Case: {is_snake}")