[Tutor] Splitting at a capital letter in a string

Bill Campbell bill at celestial.net
Sat Oct 30 05:14:35 CEST 2004

On Fri, Oct 29, 2004, Douglas N. Shawhan wrote:
>Is there a way to use the string module to split a string at a capital, 
>so that:
>is rendered
>Hi Im A String With Caps
>I looked at the re module, and it looks as if it could be used for such 

One way to do this would be:

import re
pat = re.compile('([A-Z])')
s = pat.sub(r' \1', 'HiImAStringWithCaps').strip()

The strip call gets rid of the first blank inserted.

INTERNET:   bill at Celestial.COM  Bill Campbell; Celestial Systems, Inc.
UUCP:               camco!bill  PO Box 820; 6641 E. Mercer Way
FAX:            (206) 232-9186  Mercer Island, WA 98040-0820; (206) 236-1676
URL: http://www.celestial.com/

Democracy must be sometihng more than two wolves and a sheep voting on what
to have for dinner -- James Bovard

More information about the Tutor mailing list