0

I'm trying to compare strings in Python that can be different lengths. This is to do a simple version check. Here are the examples: 1.2.3, 1.2.15, 1.2.2a

I need to compare to find which one is bigger so taking example above 1.2.2a < 1.2.3 < 1.2.15. If there were only number than it would be pretty simple but the added character messes things as it is not always present. I thought that converting to base64 which makes string the same length will solve the problem but with base64 1.2.15 becomes less than 1.2.2a and 1.2.3. Maybe there is a module that already does that. Thank you.

NosIreland
  • 91
  • 2
  • 8
  • You probable want to look at the [re module](https://docs.python.org/2/library/re.html) - regular expressions will be able to isolate the letter from the numbers. What would happen, for example, if you got 1.2.2a and 1.2.2b? – asongtoruin Jul 04 '17 at 11:54
  • *"base64 which makes string the same length"*? Wat. – Stefan Pochmann Jul 04 '17 at 11:57

1 Answers1

3

The version numbers you are using appear to confirm with the semantic versioning standard. There are libraries available to handle this for you - take a look at semver or semantic_version.

holdenweb
  • 33,305
  • 7
  • 57
  • 77
  • Welcome to SO. If this answered your question (unless, of course, someone answers it better :-)) please feel free to mark it as [accepted](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). In this case it appears the question may be closed as a duplicate of [this question](https://stackoverflow.com/questions/11887762/compare-version-strings-in-python) which may give you some more ideas. – holdenweb Jul 04 '17 at 12:08
  • Thanks for quick response this is what I was looking for. – NosIreland Jul 04 '17 at 12:25