string capitalize sentence

Skip Montanaro skip at pobox.com
Thu Jun 23 19:07:26 EDT 2005


    dimitri> I came up with the following solution:

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

No need for the replace().  Just split on ". ".  No need for a for loop
either, or the temp variable cap.  Use a list comprehension.  The result is:

    print ". ".join([s.capitalize() for s in a.split(". ")])

Skip



More information about the Python-list mailing list