+ 1
Python--How do you convert a string input to a number?
(As long as the string contains a number...)
4 Antworten
+ 4
#sring-->integer
a = int(input("Enter a number"))
#string --> float
b = float(input("Enter a number"))
+ 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"))
+ 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.
+ 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