Skip to content

Commit

Permalink
Add new snippet: Splitsies
Browse files Browse the repository at this point in the history
  • Loading branch information
satwikkansal committed Jun 8, 2019
1 parent 1a3a019 commit 36f61e4
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,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\.
Expand Down Expand Up @@ -702,7 +740,7 @@ SyntaxError: invalid syntax

---

### ▶ Strings and the backslashes\
### ▶ Strings and the backslashes\ ^

**Output:**
```py
Expand Down

0 comments on commit 36f61e4

Please sign in to comment.