I am trying to split my string str = 'I HAVE 6'
in two parts. The output should be
['I HAVE','6']
There is no info about the length of the words given as input.
One way do this is to split it in three and add the first two. Is there any other way?
You can use rsplit
, which splits from the right, with a maxsplit
of 1
:
s = 'I HAVE 6'
s.rsplit(maxsplit=1) # or (' ', 1)
['I HAVE', '6']
As an aside, don't use the name str
. It masks the built-in class str
and will lead to errors down the line.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With