Python--How do you convert a string input to a number? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Python--How do you convert a string input to a number?

(As long as the string contains a number...)

20th Feb 2017, 6:43 PM
Jeremie
Jeremie - avatar
4 Answers
+ 4
#sring-->integer a = int(input("Enter a number")) #string --> float b = float(input("Enter a number"))
20th Feb 2017, 6:52 PM
Samuel Šúr
Samuel Šúr - avatar
+ 4
A custom function to "auto" cast number to int or float, according to what's the better adapted: def str2num(s): if "." in s: return float(s) else: return int(s) n = str2num(input("Enter a number"))
21st Feb 2017, 5:30 AM
visph
visph - avatar
+ 3
Whoops! I forgot that this was in one of the beginner lessons... (Basic Concepts>Type conversion). Combine that with getting input. Problem solved. also, I found this: object = int(input("Please give me input")) --an abbreviated way to put it.
20th Feb 2017, 7:09 PM
Jeremie
Jeremie - avatar
+ 1
basically string means the no. which is in double inverted commas. to convert in into a no. u must Easter or delete double inverted commas or just write int and bracket and your no. in double indeed commas..... that's all
20th Feb 2017, 7:15 PM
nikhil musale
nikhil musale - avatar