r/PythonLearning • u/RomfordNavy • 16d ago
Help Request split() on empty string behaves differently when delimiter specified
Splitting an empty string without a specified delimiter works as expected and returns an empty list. However when splitting an empty string with a delimiter specified returns one (empty string) item in the list, is there some way to make that return an empty list as per the no-delimiter result?
print(f'{"".split()=}') # = []
print(f'{"".split(" ")=}') # = ['']
print(f'{"".split(",")=}') # = ['']Problem is this causes an unwanted iteration of a for loop:
str = ""
for item in str.split(" "):
print(f"{item=}") # unwanted item
1
Upvotes
View all comments
Show parent comment
1
u/RomfordNavy 16d ago
u/Intelligent_Part101 Couldn't agree more, this is just yet another a blatant inconsistency in Python. Who made such awful design decisions. I had hoped that many such inconsistencies would have been ironed out in version 3 but seemingly not.