DEV Community

BC
BC

Posted on

2 common mistakes on using split and splitlines function - Python Tips

split() and split(" ") and different:

>>> a = "hello     world"
>>> a.split()
['hello', 'world']
>>>
>>> a.split(" ")
['hello', '', '', '', '', 'world']
Enter fullscreen mode Exit fullscreen mode

splitlines and split("\n") are different:

>>> "".splitlines() # this will return an empty list
[]
>>> "".split("\n")  # this will return an one-element list
['']
>>>
>>> "my line\n".splitlines()
['my line']
>>> "my line\n".split("\n")
['my line', '']
Enter fullscreen mode Exit fullscreen mode

Top comments (0)