Super learner
This user is a Super Learner. To become a Super Learner, you need to reach Level 8.
Last answered:

01 Nov 2024

Posted on:

30 Oct 2024

0

Resolved: str.split() is not the same as str.split(' ')

I would like to present the detail that .split() is not the same as .split(' '). The comparison of 'Prices per unit'.slpit() vs 'Prices per unit'.split(' ') at minute 3:40 approx. of lesson Exploring Python Strings Methods - Part I of chapter 2 could lead to confusion. Nothing better than a couple of examples to see the difference:



And reaffirm the concept with the text of the manual on this method:
" If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns []."
- https://docs.python.org/3/library/stdtypes.html#string-methods
1 answers ( 1 marked as helpful)
Instructor
Posted on:

01 Nov 2024

0
Hi Jorge!
Thanks for reaching out.

You are right. Using split() without an argument treats consecutive whitespace as a single separator, while split(' ') treats only single spaces as separators, keeping multiple spaces and leading/trailing spaces as empty strings. The example clarifies this difference well.

Hope this helps.
Best,
Tsvetelin

Submit an answer