Imagine a string containing (comma) separated elements, e.g. a version string
version_str = "3,1,4,159"
which might contain one or more elements more or less:
version_str = "3,1,4"
or
version_str = "3,1,4,159,appendix"
And I want to separate these elements like this:
major, minor, patch, revision, appendix = version_str.split(',')
Then of course I get an ValueError
because the number of extracted elements does not always match.
Is there a way to extend
the result of split()
, e.g. like this:
version_str.split(',', min_elements=5)
or
version_str.split(',').extend(5, default='')
?
Example:
>>> '3,1,4'.split(',', min_elements=5)
['3', '1', '4', '', '']
>>> '3,1,4,159,dev'.split(',', min_elements=5)
['3', '1', '4', '159', 'dev']
Of course I can add elements manually afterwards or read elements conditionally, but I'm interested in the pythonic one-liner.