Python: Replace integers by floats in string -


i'm looking way, replace every number inside string float number. i'd turn this: "3/1" this: "3.0/1.0" there way this?

you can use re.sub :

>>> s="3/1"  >>> import re >>> re.sub(r'(\d+)',r'\1.0',s) '3.0/1.0' >>> s="334/14"  >>> re.sub(r'(\d+)',r'\1.0',s) '334.0/14.0' 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -