Small inconsistency between string.split and "".split
Peter Hansen
peter at engcorp.com
Mon Sep 13 13:09:26 EDT 2004
More information about the Python-list mailing list
Mon Sep 13 13:09:26 EDT 2004
- Previous message (by thread): Small inconsistency between string.split and "".split
- Next message (by thread): Small inconsistency between string.split and "".split
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Carlos Ribeiro wrote:
> While writing a small program to help other poster at c.l.py, I found
> a small inconsistency between the handling of keyword parameters of
> string.split() and the split() method of strings. I wonder if someone
> else had ever stumbled on it before, and if it has a good reason to
> work like it is.
>
> Both implementations take two parameters: the separator character and
> the max number of splits (maxsplit). However, string.split() accept
> maxsplit as a keyword parameter, while mystring.split() doesn't. In my
> case, it meant that I had to resort to string.split() in my example,
> in order to avoid having to deal with the separator.
Works here:
c:\>python
Python 2.3.4 (#53, May 25 2004, 21:17:02) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> s = 'this is my string'
>>> s.split()
['this', 'is', 'my', 'string']
>>> s.split('s')
['thi', ' i', ' my ', 'tring']
>>> s.split('s', 1)
['thi', ' is my string']
>>> s.split('s', 2)
['thi', ' i', ' my string']
> ** BTW, I had to avoid dealing with the separator for another annoying
> reason: I thought that I could do something like this:
>
> mystring.split(string.whitespace, 2)
>
> to preserve the default whitespace detecting behavior. But it won't
> work this way with neither implementation of split().
I think this works though:
>>> s.split(None, 2)
['this', 'is', 'my string']
>>> s.split(None, 1)
['this', 'is my string']
-Peter
- Previous message (by thread): Small inconsistency between string.split and "".split
- Next message (by thread): Small inconsistency between string.split and "".split
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
More information about the Python-list mailing list