[SOLVED] How to get integer or float values from a string in Python? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

[SOLVED] How to get integer or float values from a string in Python?

Hi, I want to create a program with Python that can calculate fahrenheit to celsius and celsius to fahrenheit according to user input. If the user gives 10C as string input, then the program will understand that the user wants to convert 10° Celsius (C) to Fahrenheit (F) (because user inputted C after 10). And then the program will take 10 as an integer or float and it will convert 10° celsius to fahrenheit. In the same way, if the user gives 10F as input, then the program will understand that the user wants to convert 10° Fahrenheit (F) to Fahrenheit (C) (because user inputted F after 10). And then the program will take 10 as an integer or float and it will convert 10° fahrenheit to celcius. If I want to do this, I have to take only 10 as integer or faulting number from the sting input 10C or 10F given by the user. How do I do it now? Example: 1. Input : 15.6C Expected Output: >>> 50.9°F 2. Input : 50.9F Expected Output: >>> 15.6°C [SOLVED] https://code.sololearn.com/cX53nLKNXOk2/?ref=app

13th Aug 2021, 10:39 AM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
19 Answers
+ 10
Using regex. a=input() import re b=re.match("\d+.\d+",a) print(b[0])
13th Aug 2021, 10:51 AM
Abhay
Abhay - avatar
+ 10
MD. Ferdous Ibne Abu Bakar , there is also a possible solution to do it without regex, even if this is the preferred way. but before we can help you to solve this task, we need to see your code to find out where the issue is. so please post us your attempt. it does not matter if your try is working perfectly or not. thanks for your understanding!
13th Aug 2021, 11:07 AM
Lothar
Lothar - avatar
+ 9
Shadoff , great and clean code! the only doubt i have is that the code uses int(....) to convert number part of input. the input sample shows a decimal place like 15.6C. in this case the int conversion will fail. from my point of view float(...) would be better.
13th Aug 2021, 1:04 PM
Lothar
Lothar - avatar
+ 5
if thats the set format you can slice the string like string[:-1]
13th Aug 2021, 11:05 AM
Abs Sh
Abs Sh - avatar
+ 5
x = input() if x.endswith("F"): int(x[:-1]) do something elif x.endswith("C"): int(x[:-1]) do another thing else: print("incorrect input, try again")
13th Aug 2021, 11:31 AM
Shadoff
Shadoff - avatar
+ 3
Set your input as a string, IE: temperature = input() or 15.6C Take all of the string input except the last char from temperature and turn it into a float, IE: value = float(temperature[0:-1] Then slice the last char from the input and compare using if /else, IE: if temperature[-1] == "C": print(your formula here) elif -> as above == "F": print(your formula here) else: print(optional statements if required)
13th Aug 2021, 11:27 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 3
MD. Ferdous Ibne Abu Bakar little correction in pattern, b=re.match("\d+[.]\d+|\d+",a) In previous pattern , since . is treated as any character it would match a input like 12a45 as well . You need to put it inside square brackets so that is interpreted literally as ".". Also | stands for or . Either it will match float or integer pattern (\d+).
13th Aug 2021, 11:27 AM
Abhay
Abhay - avatar
+ 3
Shadoff Niiice! Much cleaner solution
13th Aug 2021, 11:33 AM
Rik Wittkopp
Rik Wittkopp - avatar
+ 2
Show your try. And I'll give you code sweet pie
13th Aug 2021, 11:18 AM
Shadoff
Shadoff - avatar
+ 2
Shadoff Thanks man 😀
13th Aug 2021, 11:34 AM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
+ 2
Lothar you are right
13th Aug 2021, 1:06 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
13th Aug 2021, 1:35 PM
Shadoff
Shadoff - avatar
+ 2
https://code.sololearn.com/cw9ehw1jHL40/?ref=app
14th Aug 2021, 2:59 PM
Chris Jonathan
Chris Jonathan - avatar
+ 1
A little bug in my code. If I input input 10ABC it also run!!! 😐 Help me to fix it!!! https://code.sololearn.com/cX53nLKNXOk2/?ref=app
13th Aug 2021, 1:32 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
+ 1
Thank you 🥰🥰 Shadoff
13th Aug 2021, 1:41 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
+ 1
Wait as i post my code
14th Aug 2021, 2:55 PM
Chris Jonathan
Chris Jonathan - avatar
+ 1
Thanks
14th Aug 2021, 3:02 PM
Chris Jonathan
Chris Jonathan - avatar
0
Thanks you very much guys... 😍🥰 Finally I made this↓↓ https://code.sololearn.com/cX53nLKNXOk2/?ref=app
13th Aug 2021, 11:18 AM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar
14th Aug 2021, 3:01 PM
Fꫀⲅძ᥆͟ᥙ᥉᯽
Fꫀⲅძ᥆͟ᥙ᥉᯽ - avatar