Skip to content

Commit c52919b

Browse files
committed
Add new snippet: Splitsies
Resolves satwikkansal#80
1 parent 092dd09 commit c52919b

File tree

1 file changed

+39
-1
lines changed

1 file changed

+39
-1
lines changed

README.md

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,44 @@ Makes sense, right?
207207
208208
---
209209
210+
### ▶ Splitsies ^
211+
212+
```py
213+
>>> 'a'.split()
214+
['a']
215+
216+
# is same as
217+
>>> 'a'.split(' ')
218+
['a']
219+
220+
# but
221+
>>> len(''.split())
222+
0
223+
224+
# isn't the same as
225+
>>> len(''.split(' '))
226+
1
227+
```
228+
229+
#### 💡 Explanation:
230+
231+
- It might appear at first that the default seperator for split is a single space `' '`, but as per the [docs](https://docs.python.org/2.7/library/stdtypes.html#str.split),
232+
> 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 `[]`.
233+
> If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, `'1,,2'.split(',')` returns `['1', '', '2']`). Splitting an empty string with a specified separator returns `['']`.
234+
- Noticing how the leading and trailing whitespaces are handled in the following snippet will make things clear,
235+
```py
236+
>>> ' a '.split(' ')
237+
['', 'a', '']
238+
>>> ' a '.split()
239+
['a']
240+
>>> ''.split(' ')
241+
['']
242+
```
243+
244+
---
245+
246+
247+
210248
### ▶ Time for some hash brownies!
211249
212250
1\.
@@ -702,7 +740,7 @@ SyntaxError: invalid syntax
702740
703741
---
704742
705-
### ▶ Strings and the backslashes\
743+
### ▶ Strings and the backslashes\ ^
706744
707745
**Output:**
708746
```py

0 commit comments

Comments
 (0)