I tried a simple line-splitting example, but I get some unexpected behavior. Here is a sample code:
def split_string(source,splitlist): for delim in splitlist: source = source.replace(delim, ' ') return source.split(' ') out = split_string("This is a test-of the,string separation-code!", " ,!-") print out >>> ['This', 'is', 'a', 'test', 'of', 'the', 'string', 'separation', 'code', '']
As you can see, I got an extra blank line at the end of the list when I use space as a separator argument for the split () function. However, if I do not pass a single argument to the split () function, I did not get an empty line at the end of the output list.
From what I read in python docs, they said that the default argument to split () is a space. So why, when I explicitly pass "as a delimiter", does it create an empty line at the end of the output list?
source share