e.g. I have Name : John Frank Smith
What I want is to seperate by first space
so array will be [0]=John [1]=Frank Smith
what I tried, I replace space by ~ and tried to split by regex.
import re
s="John~Frank~Smith"
l=re.compile(r'/~(.+)?/').split(s)
output is:
['John~Frank~Smith']
How can I achieve as described above?
first I don't know how to put space in regex.
Use str.split() with the maxsplit parameter:
>>> s = "John Frank Smith"
>>> s.split(None, 1)
['John', 'Frank Smith']
Note: This will split on multiple occurrences of whitespace, so a string like
John Frank Smith
would give the same result. If you only want a single space as a separator, use s.split(' ', 1).
If you want to use a regex:
>>> re.split(r'~', "John~Frank~Smith",1)
['John', 'Frank~Smith']
The ~ are from your example.
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