Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: str.split() - is it possible to only specify the "limit" parameter?

Tags:

python

string

I want to split a string on whitespaces (default behavior), but I want it to split it only once - I.e. I want it to return an array with 2 items at most.

If it is not possible - i.e. if for specifying the limit I have to also specify the pattern - could you please tell how to specify the default one?

like image 991
SomethingSomething Avatar asked May 25 '15 11:05

SomethingSomething


People also ask

How do you limit a split in Python?

Splitting the String by Passing the Maxsplit Parameter. The maximum number of splits that a split() function can perform on a given string or line can be specified using the maxsplit parameter and passed as an argument to the split() function.

How does split () work in Python?

The split() method splits a string into a list. You can specify the separator, default separator is any whitespace. Note: When maxsplit is specified, the list will contain the specified number of elements plus one.

Can split () take multiple arguments?

split() method accepts two arguments. The first optional argument is separator , which specifies what kind of separator to use for splitting the string. If this argument is not provided, the default value is any whitespace, meaning the string will split whenever .

How does STR split work?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.


2 Answers

Split string on consecutive whitespace at most maxsplit times††

Resulting list will contain no leading or trailing empty strings ("") if the string has leading or trailing whitespace
†† Splits are made left to right. To split the other way (right to left), use the str.rsplit() method (requires Python 2.4+)


Python 2

str.split(sep[, maxsplit]])

Use str.split(None, maxsplit)

Note:
Specifying sep as None not specifying sep

str.split(None, -1) str.split() str.split(None)


Python 3

str.split(sep=None, maxsplit=-1)

Option A: Stick with positional arguments (Python 2 option): str.split(None, maxsplit)
>>> ' 4 2 0 '.split(None, 420)
['4', '2', '0']

Option B (personal preference, using keyword arguments): str.split(maxsplit=maxsplit)

>>> ' 4 2 0 '.split(maxsplit=420)` 
['4', '2', '0']
like image 164
YenForYang Avatar answered Sep 18 '22 17:09

YenForYang


This works:

>>> 'a b c'.split(None, 1)
['a', 'b c']

The docstring:

S.split(sep=None, maxsplit=-1) -> list of strings

Return a list of the words in S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.

You should explore at the interactive prompt:

>>> help('a'.split)

In IPython just use a question mark:

In [1]:  s = 'a'
In [2]:  s.split?

I would suggest using IPython and especially the Notebook. This makes this kind of exploration much more convenient.

like image 21
Mike Müller Avatar answered Sep 18 '22 17:09

Mike Müller