The following code:
str = 'Welcome\nto\nPythonExamples\nWelcome\nto\nPythonExamples'
chunks = str.split('\n')
print(chunks)
Correctly prints out:
['Welcome', 'to', 'PythonExamples', 'Welcome', 'to', 'PythonExamples']
I want to split the string into strings that start with 'Welcome\n' so I have tried the following:
str = 'Welcome\nto\nPythonExamples\nWelcome\nto\nPythonExamples'
chunks = str.split('Welcome\n')
print(chunks)
But this prints out:
['', 'to\nPythonExamples\n', 'to\nPythonExamples']
Notice how the first entry is empty. How can I split it up correctly so that the output is?
['to\nPythonExamples\n', 'to\nPythonExamples']
If I understand correctly you want to avoid empty strings. You can just use list comprehension, do this:
chunks = [x for x in str.split('Welcome\n') if x]
Should solve your problem. Why?
First of all, the list comprehension adds if x
in the end, this means that it will include in the list only truthy values (or rather, will omit falsy values).
But why did you get ''
in the first place? It would be the easier to point you at the source code for split
:
while (maxcount-- > 0) {
pos = FASTSEARCH(str+i, str_len-i, sep, sep_len, -1, FAST_SEARCH);
if (pos < 0)
break;
j = i + pos;
SPLIT_ADD(str, i, j);
i = j + sep_len;
}
Basically, split function looks for the next occurrence of sep
in split(sep)
and derives a substring from last occurrence to pos(it would do it maxcount
times). Since you got Welcome\n
in pos 0 and your "last occurence" is 0, it will make a substring from 0 to 0 which results in an empty string.
By the way, you would also get empty string for such string:
'Welcome\nWelcome\nto\nPythonExamples\nWelcome\nto\nPythonExamples'
results for your code, without my change:
['', '', 'to\nPythonExamples\n', 'to\nPythonExamples']
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