+ 1

Can anyone please show me how to do type-conversion from string to float using simple python coding?

8th Jun 2018, 6:12 PM
Alexander
5 Respuestas
+ 5
a = float("45.678") ^ - the a variable is a float now
8th Jun 2018, 6:22 PM
Kuba Siekierzyński
Kuba Siekierzyński - avatar
+ 3
To explicitly convert a string into a float, use the float() function. "Python Type Conversion and Type Casting" https://www.programiz.com/python-programming/type-conversion-and-casting Example: # initializing string s = "10010" # printing string converting to float e = float(s) print ("After converting to float : ", end="") print (e)
8th Jun 2018, 6:21 PM
Rahul George
Rahul George - avatar
+ 2
Alexander - Code after correction: print (float("enter a number:")+float("enter another number")) The resulting error arises since you can't convert strings composed of characters other than numbers into an int or float value.
8th Jun 2018, 6:30 PM
Rahul George
Rahul George - avatar
0
where is the mistake here= print (float(input("enter a number:")))+(float(input("enter another number"))))
8th Jun 2018, 6:26 PM
Alexander
0
try this: print((float(input()))+(float(input()))) #count ur (), u’ll see the error
8th Jun 2018, 10:05 PM
Flash