Reputation: 83
The idea is the following:
string = 'ABC DEF GHI JK LMNO P'
list = string.split()
print(list)
Output:
ABC DEF
GHI JK
LMNO P
Obviously that doesn't quite work. Is there a trick with .split
to ignore single spaces when splitting a string?
Upvotes: 1
Views: 1846
Reputation: 111
If you want to do this without using regex then you can just split on spaces like you were doing and filter the results. Like so:
astring = 'ABC DEF GHI JK LMNO P'
def strip_spaces(astring):
temp = astring.split(" ")
return [element for element in temp if len(element) != 0]
print(strip_spaces(astring))
# Output: ['ABC', 'DEF', 'GHI', 'JK', 'LMNO', 'P']
Upvotes: 0
Reputation: 2299
This is the sort of problem where regular expressions excel. So let's construct a regex to find all the spaces, that have more than one space character. \s
matches spaces, so let's go with that:
\s
And to match N-or-more than something in regex, you put a {N,}
after the expression. So, let's put {2,}
in to match for 2-or-more:
\s{2,}
Now that we have our regular expression, we need a regular expression parser. Python comes with one built in. Python's regex module also comes with a function that will split every time the regular expression pings on a match. So, we do:
import re # This is the built-in regex module
string = "ABC DEF GHI JK LMNO P"
my_list = re.split("\s{2,}", string)
Unrelated to this question, note how I changed your variable from list
to my_list
. This is because list
is a built-in keyword in Python, that you don't want to over-write.
Upvotes: 2
Reputation: 57033
Use regular expressions to split by two or more spaces:
import re
re.split("\s{2,}", string)
#['ABC DEF', 'GHI JK', 'LMNO P']
Upvotes: 2