titlecase

2.4.1 · active · verified Wed Apr 15

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.

Warnings

Install

Imports

Quickstart

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}'")

view raw JSON →