I want to split the string on multiple spaces but not on single space.
I have tried string.split() but it splits on every single space
here is my code
string='hi i am kaveer and i am a student'
string.split()
i expected the result
['hi i am','kaveer','and i am a','student']
but actual result is
['hi','i','am','kaveer','and','i','am','a','student']
You can make a regular expression that matches 2 or more spaces and use re.split()
to split on the match:
import re
s='hi i am kaveer'
re.split(r'\s{2,}', s)
result
['hi i am', 'kaveer']
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