35

I have a string

s = 'abcd qwrre qwedsasd zxcwsacds'

I want to split any string in only two parts at the first occurrence of a whitespace. i.e. a='abcd' and b='qwrre qwedsasd zxcwsacds'

If I use a, b=split(' ') it gives me an error because there are too many values to unpack.

Olivier Melançon
  • 21,584
  • 4
  • 41
  • 73
Abhishek Velankar
  • 461
  • 1
  • 4
  • 8

9 Answers9

49

You could use a,b = split(' ', 1).

The second argument 1 is the maximum number of splits that would be done.

s = 'abcd efgh hijk'
a,b = s.split(' ', 1)
print(a) #abcd
print(b) #efgh hijk

For more information on the string split function, see str.split in the manual.

ilkkachu
  • 6,221
  • 16
  • 30
TMK
  • 650
  • 6
  • 15
13

From the Python docs

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

Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).

'1 2 3'.split(maxsplit=1)
# ['1', '2 3']
Community
  • 1
  • 1
ajxs
  • 3,347
  • 2
  • 18
  • 33
  • nice one - is there also a minsplit or something to make this work from reverse? – sudonym Jun 14 '18 at 02:35
  • I tried testing negative values but that didn't yield that kind of result... I guess it would be running the risk of not being able to split a string a minimum of x ways. – ajxs Jun 14 '18 at 02:40
  • @sudonym I'm not sure what `minsplit` would do. What is the expected result of `"something".split(minsplit=2)`? – Adam Smith Jun 14 '18 at 02:40
  • the operation in reverse, i.e. just split after the first/second last occurrence? I will check the docs. – sudonym Jun 14 '18 at 02:44
  • 3
    @sudonym that's `str.rsplit` -- split from the right, not the left. – Adam Smith Jun 14 '18 at 04:42
12

You can use a standard string method partition which searches for a given separator and returns a 3-tuple consisting of string part before it, the separator itself, and the part after it.

>>> s = 'abcd qwrre qwedsasd zxcwsacds'
>>> s.partition(' ')
('abcd', ' ', 'qwrre qwedsasd zxcwsacds')
constt
  • 2,250
  • 1
  • 17
  • 18
5

Try this:

s = 'abcd qwrre qwedsasd zxcwsacds'
s1 = s.split()[0]
s2 = ' '.join(s.split()[1:])
print(s1)
print(s2)

Output:

abcd
qwrre qwedsasd zxcwsacds

Or:

new_s = ''.join([' s ' if i.isspace() else i for i in s])
a,b = new_s.replace(' s','',1).split(' s ')
print(a)
print(b)

Output:

abcd
qwrre qwedsasd zxcwsacds

Or even better split also for tabs and newlines:

a,b = s.split(None,1)
print(a)
print(b)

Output:

abcd
qwrre qwedsasd zxcwsacds
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
5

You're missing one more parameter in your split, the number of occurrences, try this;

s='abcd qwrre qwedsasd zxcwsacds'
>>> a, b = s.split(' ', 1)
>>> print(a, b)
Zack Atama
  • 81
  • 7
3

1.You can split the string like this.

a = temp.substring(0, s.indexOf(' '));
b = temp.substring(s.indexOf(' ') + 1);

2.or you can do:

a = s[0: s.find(' ')]
b = s[s.find(' ') + 1: ]

3.

a,b = s.split(' ', 1)
print(a)
print(b) 
Rajat Jain
  • 1,339
  • 2
  • 16
  • 29
3

You can solve this problem by using python "star expression".

s='abcd qwrre qwedsasd zxcwsacds'
a = s.split()
first, *second = a
second = " ".join(second)

"first" takes the first element of the list and " *second" takes the remaining elements.

>>> first
'abcd'
>>> second
'qwrre', 'qwedsasd', 'zxcwsacds'
JJ123
  • 573
  • 1
  • 4
  • 18
2

So, there's a second parameter you can pass as many have pointed out:

>>> a, b = "foo bar".split(' ', 1)

but nobody's pointing this out:

>>> a, b = "foobar".split(' ', 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 1 value to unpack

You don't want to be using this because it's unsafe unless you're guaranteed to have a string that has only the one space in it. It's better to split it and then check for how many splits you've got and then unpack them:

>>> parts = "foo bar".split(' ', 1)
>>> if len(parts) == 2:
>>>    a, b = parts 
mroman
  • 1,354
  • 9
  • 14
1
#!python2

s='abcd qwrre qwedsasd zxcwsacds'

s1 = s[0: s.find(' ')]
s2 = s[s.find(' ') + 1: ]

print s1
print s2
'''
abcd
qwrre qwedsasd zxcwsacds
'''
Michael Swartz
  • 858
  • 2
  • 15
  • 27