Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python title() with apostrophes

Is there a way to use .title() to get the correct output from a title with apostrophes? For example:

"john's school".title() --> "John'S School" 

How would I get the correct title here, "John's School" ?

like image 345
David542 Avatar asked Nov 20 '11 06:11

David542


People also ask

How does Python deal with apostrophes?

By using the escape character \" we are able to use double quotes to enclose a string that includes text quoted between double quotes. Similarly, we can use the escape character \' to add an apostrophe in a string that is enclosed in single quotes: print('Sammy\'s balloon is red. ')

What does title () do in Python?

Python String title() Method The title() method returns a string where the first character in every word is upper case. Like a header, or a title. If the word contains a number or a symbol, the first letter after that will be converted to upper case.

How do you use title case 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 is the difference between title () and capitalize ()?

The difference between them is that Python string method title() returns a copy of the string in which the first characters of all the words are capitalized whereas the string method capitalize() returns a copy of the string in which just the first word of the entire string is capitalized.


2 Answers

If your titles do not contain several whitespace characters in a row (which would be collapsed), you can use string.capwords() instead:

>>> import string >>> string.capwords("john's school") "John's School" 

EDIT: As Chris Morgan rightfully says below, you can alleviate the whitespace collapsing issue by specifying " " in the sep argument:

>>> string.capwords("john's    school", " ") "John's    School" 
like image 90
Frédéric Hamidi Avatar answered Sep 30 '22 23:09

Frédéric Hamidi


This is difficult in the general case, because some single apostrophes are legitimately followed by an uppercase character, such as Irish names starting with "O'". string.capwords() will work in many cases, but ignores anything in quotes. string.capwords("john's principal says,'no'") will not return the result you may be expecting.

>>> capwords("John's School") "John's School" >>> capwords("john's principal says,'no'") "John's Principal Says,'no'" >>> capwords("John O'brien's School") "John O'brien's School" 

A more annoying issue is that title itself does not produce the proper results. For example, in American usage English, articles and prepositions are generally not capitalized in titles or headlines. (Chicago Manual of Style).

>>> capwords("John clears school of spiders") 'John Clears School Of Spiders' >>> "John clears school of spiders".title() 'John Clears School Of Spiders' 

You can easy_install the titlecase module that will be much more useful to you, and does what you like, without capwords's issues. There are still many edge cases, of course, but you'll get much further without worrying too much about a personally-written version.

>>> titlecase("John clears school of spiders") 'John Clears School of Spiders' 
like image 20
Art Taylor Avatar answered Sep 30 '22 22:09

Art Taylor