-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github.com:bbelderbos/bobcodesit
- Loading branch information
Showing
2 changed files
with
26 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |