2

how can I check in python if string is in correct format?
I have variable tag, and it should be in fomrat 0.0

e.g

tag = "1.0"

if tag != (is not in format **0.0**):
    print("not in tag")"
else:
    print("correct tag")"

In bash it looks like this

tag="10.0"
if [[ $tag != "${tag%.*}.${tag#*.}" ]]; then
    echo "wrong tag"
fi
joeSwan
  • 31
  • 3

2 Answers2

3

The simplest implementation is to use regular expressions:

if not re.search("^\d+\.\d+$", tag):
    #does not match
DYZ
  • 55,249
  • 10
  • 64
  • 93
1

As referenced from this post, I would recommend looking into regular expressions.

Ex:

import re
r = re.compile("^(\d*\.)?\d+$")
if r.match(tag) is not None:
   print('match')
else:
   print('not match')