Skip to main content
https://codecov.io/gh/ppannuto/python-titlecase/branch/main/graph/badge.svg?token=J1Li8uhB8q

This filter changes a given text to Title Caps, and attempts to be clever about SMALL words like a/an/the in the input. The list of “SMALL words” which are not capped comes from the New York Times Manual of Style, plus some others like ‘vs’ and ‘v’.

The filter employs some heuristics to guess abbreviations that don’t need conversion.

Original

Conversion

this is a test

This Is a Test

THIS IS A TEST

This Is a Test

this is a TEST

This Is a TEST

More examples and expected behavior for corner cases are available in the package test suite.

This library is a resurrection of Stuart Colville’s titlecase.py, which was in turn a port of John Gruber’s titlecase.pl.

Issues, updates, pull requests, etc should be directed to github.

Installation

The easiest method is to simply use pip:

(sudo) pip install titlecase

Usage

Titlecase provides only one function, simply:

>>> from titlecase import titlecase
>>> titlecase('a thing')
'A Thing'

A callback function may also be supplied, which will be called for every word:

>>> def abbreviations(word, **kwargs):
...   if word.upper() in ('TCP', 'UDP'):
...     return word.upper()
...
>>> titlecase.titlecase('a simple tcp and udp wrapper', callback=abbreviations)
'A Simple TCP and UDP Wrapper'

The callback function is supplied with an all_caps keyword argument, indicating whether the entire line of text was entirely capitalized. Returning None from the callback function will allow titlecase to process the word as normal.

Command Line Usage

Titlecase also provides a command line utility titlecase:

$ titlecase make me a title
Make Me a Title
$ echo "Can pipe and/or whatever else" | titlecase
Can Pipe and/or Whatever Else
# Or read/write files:
$ titlecase -f infile -o outfile

In addition, commonly used acronyms can be kept in a local file at ~/.titlecase.txt. This file contains one acronym per line. The acronym will be maintained in the title as it is provided. Once there is e.g. one line saying TCP, then it will be automatically used when used from the command line.

$ titlecase I LOVE TCP
I Love TCP

Limitations

This is a best-effort library that uses regexes to try to do intelligent things, but will have limitations. For example, it does not have the contextual awareness to distinguish acronyms from words: us (we) versus US (United States).

The regexes and titlecasing rules were written for American English. While there is basic support for Unicode characters, such that something like “El Niño” will work, it is likely that accents or non-English phrases will not be handled correctly.

If anyone has concrete solutions to improve these or other shortcomings of the library, pull requests are very welcome!

Release files for titlecase 2.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for titlecase 2.4.1
File Size Uploaded
titlecase-2.4.1.tar.gz 14.4 kB Details

Release files / titlecase-2.4.1.tar.gz

Download URL titlecase-2.4.1.tar.gz
Size 14.4 kB
Tags Source
SHA-256 checksum
How to use checksums
7d83a277ccbbda11a2944e78a63e5ccaf3d32f828c594312e4862f9a07f635f5
BLAKE2b-256 checksum
How to use checksums
631704d2d3e30e2bc5a3eefa1060b08e3fb628510440f938eaecabbe08976a26
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/65.6.3 requests-toolbelt/0.9.1 tqdm/4.56.0 CPython/3.9.17

Release history Release notifications | RSS feed

This release

2.4.1 This release

1 release file

2.4

1 release file

2.3

2 release files

2.2.0

1 release file

2.1.0

1 release file

2.0.0

1 release file

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.13.1

1 release file

0.13.0

2 release files

0.12.0

2 release files

0.11.0

2 release files

0.10.0

2 release files

0.9.0

2 release files

0.8.2

1 release file

0.8.1

1 release file

0.8.0

1 release file

0.7.2

2 release files

0.7.1

1 release file

0.7.0

0.6.0

1 release file

0.5.1

2 release files

0.4

3 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page