Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String split with default delimiter vs user defined delimiter

Tags:

python

split

I tried a simple example with string split, but get some unexpected behavior. Here is the 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 empty string at the end of the list when I use space as delimiter argument for split() function. However, if I don't pass in any argument for split() function, I got no empty string at the end of the output list.

From what I read in python docs, they said the default argument for split() is space. So, why when I explicitly pass in a ' ' as delimiter, it creates an empty string at the end of the output list?

like image 431
OhMyGosh Avatar asked May 16 '15 03:05

OhMyGosh


People also ask

What is the default delimiter when using the split method on a string?

The split() method splits a string into a list. You can specify the separator, default separator is any whitespace.

What is difference between Split and Rsplit?

The only difference between split() and rsplit() is the use of the maxsplit argument. If the maxsplit argument is set, the rsplit() function splits a string from the right side (from the final character), whereas the split() method splits from the left side (from the first character).

Does split () alter the original string?

Note: The split() method does not change the original string. Remember – JavaScript strings are immutable. The split method divides a string into a set of substrings, maintaining the substrings in the same order in which they appear in the original string. The method returns the substrings in the form of an array.

Which method is used to break a string into an array of substrings based on a separator?

The split() method splits a string into an array of substrings. The split() method returns the new array. The split() method does not change the original string. If (" ") is used as separator, the string is split between words.


1 Answers

The docs:

If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace.

like image 172
perreal Avatar answered Sep 22 '22 23:09

perreal