Convert string format python I want to fix number of digits after '.' in digit represented by string.
Is there better solution than mine?
My code
def convert(s):
    &quot;&quot;&quot; Convert 1234.12345678 to 1234.123&quot;&quot;&quot;
    vec = s.split('.')
    if len(vec) == 2 and len(vec[1]) &gt; 3:
        return vec[0] + '.' + vec[1][:3]
    return s
In fact this would be sufficient:
return '{0:1.3f}'.format(s)

(provided that the argument passed is a float).
There is no need for a function.
The above will return a string, if you want to return a float then use the float function:
return float('{0:1.3f}'.format(s))

Or even shorter, taking advantage of F-strings. I prefer this option personally:
return f'{s:.3f}'

Again, this returns a string but you can cast it to a float if necessary.
As a reference: Python f-string tutorial