In this article Xiaobian to share with you about Python instance method to convert the string to float and related knowledge, need friends refer to the
How to convert string to float Python? Simple steps, let you easily solve.
Open Software python new project, as shown in FIG
context menu .py files created, as shown
in step file input code is as follows:
def string_to_float(str):
return float(str)
if __name__ == '__main__':
str = '3.1415926'
res = string_to_float(str)
print(res + 1)
After the blank menu, select [Run 'test']
See results are shown in FIG operation
needs to be converted to string float, you can directly call the function of FIG.
Knowledge Point Spread
python str converted into float
Using a map and reduce str2float write function, the string '123.456' into float 123.456
fromfunctoolsimportreduce
defstr2float(s):
returnreduce(lambdax,y:x+int2dec(y),map(str2int,s.split('.')))
defchar2num(s):
return{'0':0,'1':1,'2':2,'3':3,'4':4,'5':5,'6':6,'7':7,'8':8,'9':9}[s]
defstr2int(s):
returnreduce(lambdax,y:x*10+y,map(char2num,s))
defintLen(i):
returnlen('%d'%i)
defint2dec(i):
returni/(10**intLen(i))
print(str2float('123.456'))
We recommend the python learning sites, click to enter , to see how old the program is to learn! From basic python script, reptiles, django, data mining, programming techniques, work experience, as well as senior careful study of small python partners to combat finishing zero-based information projects! Every day, Python programmers explain the timing of technology, sharing some learning methods and the need to pay attention to small details
than this is how Python convert string to float all the knowledge points