Why does split() return more elements than split(“ ”) on same string?

前端 未结 5 1685
闹比i
闹比i 2021-01-15 03:52

I am using split() and split(\" \") on the same string. But why is split(\" \") returning less number of elements than split()

5条回答
  •  野的像风
    2021-01-15 04:35

    If you run the help command on the split() function you'll see this:

    split(...) S.split([sep [,maxsplit]]) -> list of strings

    Return a list of the words in the string S, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done. If sep is not specified or is None, any whitespace string is a separator and empty strings are removed from the result.

    Therefore the difference between the to is that split() without specifing the delimiter will delete the empty strings while the one with the delimiter won't.

提交回复
热议问题