Titlecasing line with exceptions

Is there a standard way in Python to have a line header (i.e. words begin with uppercase characters, all remaining circled characters have lowercase letters), but leaving articles such as and , in and of with lowercase?

+82
python string title-case
Sep 16 '10 at 16:25
source share
8 answers

There are several issues with this. If you use split and join, some space characters will be ignored. The built-in capize and title methods do not ignore spaces.

 >>> 'There is a way'.title() 'There Is A Way' 

If a sentence begins with an article, you do not want the first title word to be lowercase.

Keeping this in mind:

 import re def title_except(s, exceptions): word_list = re.split(' ', s) # re.split behaves as expected final = [word_list[0].capitalize()] for word in word_list[1:]: final.append(word if word in exceptions else word.capitalize()) return " ".join(final) articles = ['a', 'an', 'of', 'the', 'is'] print title_except('there is a way', articles) # There is a Way print title_except('a whim of an elephant', articles) # A Whim of an Elephant 
+142
Sep 16 '10 at 19:16
source share

Use the titlecase.py module! Only works for English.

 >>> from titlecase import titlecase >>> titlecase('i am a foobar bazbar') 'I Am a Foobar Bazbar' 

GitHub: https://github.com/ppannuto/python-titlecase

+47
Sep 16 '10 at 17:18
source share

The following methods are available:

 >>> mytext = u'i am a foobar bazbar' >>> print mytext.capitalize() I am a foobar bazbar >>> print mytext.title() I Am A Foobar Bazbar 

There is no lowercase option. You should encode them yourself, possibly using a list of articles that you want to reduce.

+20
Sep 16 '10 at 16:34
source share

Stuart Colville created a Python port for the Perl script written by John Gruber to convert strings to case-sensitive headers, but avoids using rule-based capital letters from the New York Times style guide and also takes into account some special cases.

Some of the tricks of these scenarios are:

  • they are written with a small letter, for example, if, in, on, etc., but do not write them with a capital letter if they are mistakenly entered with a capital letter.

  • scripts suggest that words with capital letters other than the first character are already spelled correctly. This means that they will leave the word “iTunes” alone, instead of interrupting it in “iTunes” or, worse, “iTunes”.

  • they skip any words with linear dots; "example.com" and "del.icio.us" will remain lowercase.

  • they have hard-coded hacks specifically designed to work with odd cases, such as “AT & T” and “Q & A”, both of which contain small words (at and a), which usually should be lowercase.

  • The first and last word of the heading is always capitalized, so input such as “Fear nothing” will be turned into “Fear nothing”.

  • The small word after the colon will be written in capital letters.

You can download it here .

+12
Feb 10 2018-12-12T00:
source share
 capitalize (word) 

It has to be done. I understand it differently.

 >>> mytext = u'i am a foobar bazbar' >>> mytext.capitalize() u'I am a foobar bazbar' >>> 

Well, as said in the answer above, you have to make custom capital letters:

mytext = u'i am foobar bazbar '

 def xcaptilize(word): skipList = ['a', 'an', 'the', 'am'] if word not in skipList: return word.capitalize() return word k = mytext.split(" ") l = map(xcaptilize, k) print " ".join(l) 

Displays

 I am a Foobar Bazbar 
+3
Sep 16 '10 at 16:37
source share

The Python 2.7 header method has a flaw.

 value.title() 

Carpenter Specialist Assistant S will return when the value is "Carpenter" s Assistant

The best solution is probably one of @BioGeek using the front page from Stuart Colville. This is the same solution suggested by @Etienne.

+2
Nov 30 '14 at 18:57
source share
  not_these = ['a','the', 'of'] thestring = 'the secret of a disappointed programmer' print ' '.join(word if word in not_these else word.title() for word in thestring.capitalize().split(' ')) """Output: The Secret of a Disappointed Programmer """ 

The heading begins with a headword and does not correspond to the article.

+1
Sep 16 '10 at 17:05
source share

Single line using list comprehension and ternary operator

 reslt = " ".join([word.title() if word not in "the a on in of an" else word for word in "Wow, a python one liner for titles".split(" ")]) print(reslt) 

Structure:

for word in "Wow, a python one liner for titles".split(" ") Splits a string in a list and initiates a for loop (in list calculation)

word.title() if word not in "the a on in of an" else word uses its own title() method for the title of the case, if this is not an article

" ".join combines list items with a separator (space)

+1
Jul 07 '17 at 20:00
source share



All Articles