Skip to content

Commit

Permalink
Merge branch 'main' of github.com:bbelderbos/bobcodesit
Browse files Browse the repository at this point in the history
  • Loading branch information
bbelderbos committed Jun 27, 2024
2 parents e25fb1f + a302617 commit 5ba2d37
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ This file gets generated by [this script](index.py).
- [More performant string building](notes/20231218133029.md)
- [Removesuffix and removeprefix](notes/20240124191423.md)
- [See if a string only contains ascii characters](notes/20221216114651.md)
- [Split's maxspit optional argument](notes/20240620083150.md)
- [Splitlines](notes/20220907130613.md)
- [Startswith can receive a tuple](notes/20240130100737.md)
- [String methods over regular expressions](notes/20220908104354.md)
Expand Down
25 changes: 25 additions & 0 deletions notes/20240620083150.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# split's maxspit optional argument

`maxsplit` determines the maximum number of splits. This can be handy if the split character appears in the string multiple times. If `maxsplit` is not specified, there is no limit on the number of splits. Example:

```python
# string with comma (split character) in the one of the fields
string = "John, [email protected], Message with comma, more text"

# not what you want:
string.split(", ") ['John', '[email protected]', 'Message with comma', 'more text']

# cannot rely on number of splits
name, email, msg = string.split(", ") # ValueError: not enough values to unpack ...

# what you actually want:
string.split(", ", maxsplit=2) # ['John', '[email protected]', 'Message with comma, more text']

# now can properly unpack
name, email, msg = string.split(", ", maxsplit=2)
name # 'John'
email # '[email protected]'
msg # 'Message with comma, more text'
```

#strings

0 comments on commit 5ba2d37

Please sign in to comment.