str.title() fails with words containing apostrophes

D'Arcy Cain darcy at VybeNetworks.com
Mon Mar 6 01:31:30 EST 2017


On 2017-03-05 03:40 PM, Terry Reedy wrote:
>> import re
>>
>> def title(string):
>>     return re.sub(r"\b'\w", lambda m: m.group().lower(), string.title())
>
> Nice.  It lowercases a word char that follows an "'" that follows a word
> without an intervening non-word char. It passes this test:
> print(title("'time' isn't 'timeless'!"))
> 'Time' Isn't 'Timeless'!
>
> It guess the reason not to bake this exception into str.title is that it
> is language specific and could even be wrong if someone used "'" to
> separate words (perhaps in a different alphabet).

Or, it doesn't handle exceptions.

print title("My name is D'Arcy")

Oops.

-- 
D'Arcy J.M. Cain
Vybe Networks Inc.
http://www.VybeNetworks.com/
IM:darcy at Vex.Net VoIP: sip:darcy at VybeNetworks.com



More information about the Python-list mailing list