string capitalize sentence

Konstantin Veretennicov kveretennicov at gmail.com
Thu Jun 23 19:07:13 EDT 2005


On 6/24/05, dimitri pater <dimitri.pater at gmail.com> wrote:
>  a = 'harry is a strange guy. so is his sister, but at least she is not a
> guy. i am.'
>  b = a.replace('. ', '.')
>  splitlist = b.split('.')
>  newlist = []
>  for i in range(len(splitlist)):
>       i = ''.join(splitlist[i].capitalize() + '.'
>       newlist.append(i)
>  cap = ' '.join(newlist).replace(' .', '')
>  print cap

A simpler version would be:

>>> s = 'harry is a strange guy. so is his sister, but at least she is not a guy
. i am.'
>>> a = s.split('. ') # you can split at multicharacter strings
>>> b = (x.capitalize() for x in a) # you can use generator
expressions to generate lists
>>> '. '.join(b)
'Harry is a strange guy. So is his sister, but at least she is not a guy. I am.'

>  it doesn't work with:
>  is harry a strange guy? so is his sister, but at least she is not a guy. i
> am.

I believe you'll need re.split to accomplish that.

Regards,
- kv



More information about the Python-list mailing list