DEV Community 👩‍💻👨‍💻

Bo
Bo

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)

Update Your DEV Experience Level:

Settings

Go to your customization settings to nudge your home feed to show content more relevant to your developer experience level. 🛠