To receive notifications about scheduled maintenance, please subscribe to the mailing-list You can subscribe to the mailing-list at

Commit ef6e6da6 authored by vermeul's avatar vermeul
Browse files


parent 1af83c35
......@@ -31,6 +31,32 @@ print(string[:1].upper() + string[1:])
## Split strings
Splitting a string by specifying a delimiter is very easy:
parts = 'a sentence with many words'.split(' ')
However, when you are expecting a given number of values and want to directly assign them to the values, you'll run into ValueError:
firstname, lastname= 'Voldemort'.split(' ')
ValueError: not enough values to unpack (expected 2, got 1)
How can you assure one always gets enough values and if the lastname does not exist, it will receive a default value (e.g. an empty string)?
firstname, lastname, *_ = 'Voldemort'.split(' ') + ['']
The trick is to ensure that the right side of the assignment always returns at least 2 elements. If more elements are returned, they will be passed to the magic underscore `_` variable. The `*_` means that this magic variable is treated as an array. This page shows more tricks you can do by using this variable:
## Filtering: the `filter` function
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment