str.title question after '
Fredrik Lundh
fredrik at pythonware.com
Mon Nov 13 15:58:50 EST 2006
Leo Kislov wrote:
>> Is there an easy way to get what I want?
>
> def title_words(s):
> words = re.split('(\s+)', s)
> return ''.join(word[0:1].upper()+word[1:] for word in words)
nit: to work well also for Unicode strings using arbitrary alphabets,
you should use title() instead of upper(). a naive upper() will do the
wrong thing in some cases, as can be seen in the following example:
>>> u = u"\u01C9"
>>> unicodedata.name(u)
'LATIN SMALL LETTER LJ'
>>> unicodedata.name(u.upper())
'LATIN CAPITAL LETTER LJ'
>>> unicodedata.name(u.title())
'LATIN CAPITAL LETTER L WITH SMALL LETTER J'
</F>
More information about the Python-list
mailing list