mirror of
https://github.com/satwikkansal/wtfpython
synced 2024-11-22 11:04:25 +01:00
Add new snippet: Splitsies
Resolves https://github.com/satwikkansal/wtfpython/issues/80
This commit is contained in:
parent
f3b96e4d7f
commit
e3118ed4da
40
README.md
vendored
40
README.md
vendored
@ -209,6 +209,44 @@ Makes sense, right?
|
||||
|
||||
---
|
||||
|
||||
### ▶ Splitsies ^
|
||||
|
||||
```py
|
||||
>>> 'a'.split()
|
||||
['a']
|
||||
|
||||
# is same as
|
||||
>>> 'a'.split(' ')
|
||||
['a']
|
||||
|
||||
# but
|
||||
>>> len(''.split())
|
||||
0
|
||||
|
||||
# isn't the same as
|
||||
>>> len(''.split(' '))
|
||||
1
|
||||
```
|
||||
|
||||
#### 💡 Explanation:
|
||||
|
||||
- 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),
|
||||
> 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 `[]`.
|
||||
> 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 `['']`.
|
||||
- Noticing how the leading and trailing whitespaces are handled in the following snippet will make things clear,
|
||||
```py
|
||||
>>> ' a '.split(' ')
|
||||
['', 'a', '']
|
||||
>>> ' a '.split()
|
||||
['a']
|
||||
>>> ''.split(' ')
|
||||
['']
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
|
||||
|
||||
### ▶ Time for some hash brownies!
|
||||
|
||||
1\.
|
||||
@ -704,7 +742,7 @@ SyntaxError: invalid syntax
|
||||
|
||||
---
|
||||
|
||||
### ▶ Strings and the backslashes\
|
||||
### ▶ Strings and the backslashes\ ^
|
||||
|
||||
**Output:**
|
||||
```py
|
||||
|
Loading…
Reference in New Issue
Block a user