Which fix removes the bug?
line = 'red green blue'
words = line.split(' ')
print(len(words)) # expected 3
- Use
line.split(' ', 3) - Use
line.partition(' ') - Call
line.split()with no argument - Call
line.strip().split(' ')
split(' ') splits on every single space, so the double space yields an empty string. split() splits on runs of whitespace and drops empties.
Play Problem Solving ▸