Registry / serialization / titlecase

titlecase

JSON →
library2.4.1pypypi✓ verified 90d ago

The `titlecase` library is a Python port of John Gruber's `titlecase.pl` script (version 2.4.1). It provides a more intelligent and stylistically correct title-casing function than Python's built-in `str.title()` method. It applies rules from the New York Times Manual of Style, handling 'small words' (like articles, conjunctions, and prepositions) and employing heuristics for abbreviations, aiming for publication-ready titles. The library maintains an active status with updates occurring as needed for bug fixes or minor enhancements.

pip install titlecase
INSTALL
IMPORT
SIG · TITLECASE
T
titlecase
serializationpythonv2.4.1
Install
3.6s avg
Import
43ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.9–3.13
musl
3.9–3.13
Install & Compatibility
Where this runs
tested against v2.4.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.10–3.920 runs
installs and imports cleanly · install 0.0s · import 0.044s · 17.8MB
glibc
py 3.10–3.920 runs
installs and imports cleanly · install 3.6s · import 0.041s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

titlecase
✓ from titlecase import titlecase
✗ import titlecase
The primary functionality is exposed directly as the 'titlecase' function from the module.

Demonstrates basic title-casing and how to use a custom callback function to handle specific abbreviations or words that should not be automatically title-cased.

from titlecase import titlecase # Basic usage text_input = "the quick brown fox jumps over the lazy dog" title_cased_text = titlecase(text_input) print(f"Original: '{text_input}'") print(f"Title cased: '{title_cased_text}'") # With a custom callback for abbreviations def custom_abbreviations(word, **kwargs): if word.upper() in ('TCP', 'UDP'): return word.upper() return None # Return None to let titlecase process normally tech_phrase = 'a simple tcp and udp wrapper' formatted_tech_phrase = titlecase(tech_phrase, callback=custom_abbreviations) print(f"Original (tech): '{tech_phrase}'") print(f"Formatted (tech): '{formatted_tech_phrase}'")
Debug
Known issues
gotchaDo not confuse with Python's built-in `str.title()` method. The built-in method capitalizes the first letter of *every* word, including small words (e.g., 'a', 'an', 'the', 'of') and incorrectly handles apostrophes (e.g., 'it's' becomes 'It'S'). The `titlecase` library is designed to address these common stylistic inaccuracies for proper title casing.
fix
Always import and use the `titlecase` function from this library for grammatically correct title casing, especially for headlines and titles.
affects: All versions
gotchaThe library's title-casing rules are primarily based on American English (New York Times Manual of Style) and use regex heuristics. This means it might not perfectly handle complex linguistic nuances, non-standard abbreviations, or phrases in other languages or specific non-American English contexts.
fix
For highly specific formatting needs, consider using the `callback` argument to define custom rules for particular words or abbreviations, or pre-process text that falls outside American English conventions.
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'titlecase'
The `titlecase` library has not been installed in the active Python environment.
fix
pip install titlecase
AttributeError: module 'titlecase' has no attribute 'title'
The primary function for title-casing in the `titlecase` library is named `titlecase`, not `title`.
fix
import titlecase
titlecase.titlecase("your string here")
TypeError: titlecase() missing 1 required positional argument: 'text'
The `titlecase.titlecase()` function requires the string to be processed as its first argument.
fix
import titlecase
titlecase.titlecase("Your string here")
ImportError: cannot import name 'title' from 'titlecase'
The main title-casing function in the `titlecase` library is named `titlecase`, not `title`.
fix
from titlecase import titlecase
Upgrade
Version history
2.4.1latest on PyPI · released Sep 23, 2023
Audit
Dependencies

No dependency data recorded yet.

Agent activity
23 hits · last 30 days
node
20
OpenAI (training)
1
Resources
titlecase — pip install titlecase · libregistry