Extracting the first two components of a version number What's the most pythonic way to extract a substring by delimiter position?  

i.e. something like slicing a string, but where the index is based on the delimiter position, rather than the character position in the string.

I have a version string "1.2.3.dev0" and I want to extract the first two components "1.2".

Currently I'm using:

&gt;&gt;&gt; delimiter = "."
&gt;&gt;&gt; version = "1.2.3.dev0"
&gt;&gt;&gt; short_version = delimiter.join((version.split(delimiter)[0:2]))
&gt;&gt;&gt; print(short_version)
1.2


Is this the simplest way to achieve this?
I can't think of a simpler way of doing the splitting.

The only thing that comes to mind is replacing [0:2] by [:2] to take the first two components of the version number, since the first index in the slice notation defaults to the beginning of a sequence object.

However, instead of using a string, you could use a namedtuple with integer elements to represent a version number, which would allow you to do comparisons more easily:

&gt;&gt;&gt; from collections import namedtuple
&gt;&gt;&gt; VersionNumber = namedtuple('VersionNumber', 'major minor')
&gt;&gt;&gt; version_str = '1.2.3.dev0'
&gt;&gt;&gt; version = VersionNumber(*map(int, version_str.split('.')[:2]))
&gt;&gt;&gt; print(version)
VersionNumber(major=1, minor=2)
&gt;&gt;&gt; if version.major &lt; 2:  # don't care about minor version
...     print('Need at least version 2.')


You could easily convert it back to a string:

&gt;&gt;&gt; '.'.join(map(str, version))
'1.2'