Split string into two parts only

Question:

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.

Asked By: Abhishek Velankar

||

Answers:

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) 
Answered By: Rajat Jain

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']
Answered By: ajxs
#!python2

s='abcd qwrre qwedsasd zxcwsacds'

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

print s1
print s2
'''
abcd
qwrre qwedsasd zxcwsacds
'''
Answered By: Michael Swartz

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.

Answered By: TMK

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')
Answered By: constt

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'
Answered By: JJ123

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
Answered By: U12-Forward

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)
Answered By: Zack Atama

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 
Answered By: mroman
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.