[Tutor] Splitting on punctuation
Albert-Jan Roskam
fomcl at yahoo.com
Mon Jun 10 10:27:23 CEST 2013
_______________________________
> From: Alan Gauld <alan.gauld at btinternet.com>
>To: tutor at python.org
>Sent: Sunday, June 9, 2013 7:48 AM
>Subject: Re: [Tutor] Splitting on punctuation
>
>
>On 09/06/13 00:44, Mike Nickey wrote:
>
>> One of the tasks is to split on punctuation passed yet I'm having a bit
>> more trouble then I expected.
>
>You do realize that split() can do that for you?
>Its not limited to splitting on whitespace?
Or use re.split:
>>> import re, string
>>> string.punctuation
'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
>>> re.split("[" + string.punctuation + "]+", "yes, but no. But: yes, no")
['yes', ' but no', ' But', ' yes', ' no']
>>>
More information about the Tutor
mailing list