June 2017
Beginner
352 pages
8h 39m
English
Another very useful string method is partition() which divides a string into three sections; the part before a separator, the separator itself, and the part after the separator:
>>> "unforgettable".partition('forget')('un', 'forget', 'table')
The partition() method returns a tuple, so this is commonly used in conjunction with tuple unpacking:
>>> departure, separator, arrival = "London:Edinburgh".partition(':')>>> departureLondon>>> arrivalEdinburgh
Often, we're not interested in capturing the separator value, so you might see the underscore variable name used. This is not treated in a special way by the Python language, but there's an unwritten convention that the underscore variable is for unused or dummy values:
Read now
Unlock full access