[Tutor] Splitting at a capital letter in a string
Bill Campbell
bill at celestial.net
Sat Oct 30 07:36:32 CEST 2004
On Fri, Oct 29, 2004, Douglas N. Shawhan wrote:
>Yep. re looks like the only way. I was just wondering if I was missing
>something in String.
>
>Thanks! That's what I'll work with!
Glad to help. I'm very much a python newbie after doing perl for over 15
years. Having python doing perl-style regular expressions is very helpful
to me.
>Bill Campbell wrote:
>
>>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:
>>>
>>>HiImAStringWithCaps
>>>
>>>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
>>>shenannigans...
>>>
>>>
>>
>>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.
>>
>>Bill
>>--
>>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
>>_______________________________________________
>>Tutor maillist - Tutor at python.org
>>http://mail.python.org/mailman/listinfo/tutor
>>
>>
>>
>
--
Bill
--
INTERNET: bill at Celestial.COM Bill Campbell; Celestial Software LLC
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/
``Nobody wants to be called common people, especially common people.''
Will Rogers
More information about the Tutor
mailing list