stupid newbie question about string and re

Hans Nowak wurmy at earthlink.net
Fri Jan 18 10:08:39 EST 2002


Bertrand Geston wrote:
> 
> Hi all,
> 
> after import re and string, I do that:
> >>> re.sub('( (.))',string.upper('\\2'),"I feel really stupid")
> 
> and I received that:
> 'Ifeelreallystupid'
> 
> I want that:
> 'IFeelReallyStupid (or even "IFeelSmart" ... but that is another story :-)
> 
> Where is the mistake please ?

Try:

>>> import string
>>> s = "I feel really stupid"
>>> string.join(map(string.capwords, string.split(s)), "")
'IFeelReallyStupid'

or

>>> string.join(string.split(string.capwords(s)), "")
'IFeelReallyStupid'

No need to use regular expressions for this...

--Hans (base64.decodestring('d3VybXlAZWFydGhsaW5rLm5ldA==') 
       # decode for email address ;-)
Site:: http://www.awaretek.com/nowak/



More information about the Python-list mailing list