>>> import re
>>> re.split(r'(\s+)', "Hello world!")
['Hello', ' ', 'world!']
"MRAB" <pyt...@mrabarnett.plus.com> wrote:
> >>> import re
> >>> re.split(r'(\s+)', "Hello world!")
> ['Hello', ' ', 'world!']
That was exactly (EXACTLY!) the solution I was looking for.
Thank you!
Malcolm
also, partition works though it returns a tuple instead of a list.
>>> s = 'hello world'
>>> s.partition(' ')
('hello', ' ', 'world')
>>>
--Tim Arnold
> also, partition works though it returns a tuple instead of a list.
> >>> s = 'hello world'
> >>> s.partition(' ')
> ('hello', ' ', 'world')
I've never used partition() before; my first thought on reading the above
was, "That's weird, it should be returning a list". Then I went and looked
at the docs. Given the description (returns specifically a 3-tuple), I
guess a tuple makes sense, but now I'm wondering what the use case was for
this method when it was invented?
Having a variant of split() which either leaves the delimiter on the end of
each word, or returns a list of alternating [word, delimiter, word,
delimiter, word] seems logical and orthogonal. In fact, partition() is
really just the hypothetical whitespace-preserving variant of split(), with
maxsplit=1, except that it returns a tuple instead of a list.
So, what was the original problem partition() was trying to solve?
http://docs.python.org/whatsnew/2.5.html
--
Aahz (aa...@pythoncraft.com) <*> http://www.pythoncraft.com/
import antigravity