Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper capitalization for titles in Python [closed]

Tags:

python

I'm looking for a library that properly capitalizes a given string by assuming it is a title. I know there is string.title(), but it capitalizes every work, not just the one's that should be capitalized. Anyone know any libraries?

Examples of words that should not be capitalized in titles:

  • Propositions (in, from, etc.)
  • Articles (a, an, the)
  • van
  • de
like image 270
Jonathan Ong Avatar asked Oct 19 '11 03:10

Jonathan Ong


People also ask

How do you capitalize titles in Python?

To make titlecased version of a string, you use the string title() method. The title() returns a copy of a string in the titlecase. The title() method converts the first character of each words to uppercase and the remaining characters in lowercase.

What does capitalize () to in Python?

The capitalize() method returns a string where the first character is upper case, and the rest is lower case.

Does true need to be capitalized Python?

True and False in Python are capitialised because they are constants and it is common practice to capitialise constants in Python. Other languages with similar naming practices tend not to follow it for whatever reason. The language is also case sensitive meaning true and True are different according to Python.

How do you capitalize a single letter in Python?

To capitalize the first letter, use the capitalize() function. This functions converts the first character to uppercase and converts the remaining characters to lowercase.


1 Answers

The term to Google for is "titlecase". The first hit is the titlecase package:

$ pip install titlecase
...
$ python
...
>>> from titlecase import titlecase
>>> titlecase('the spam and eggs')
'The Spam and Eggs'
like image 51
David Wolever Avatar answered Sep 22 '22 02:09

David Wolever