my_input = int(my_input)
There is no shorter way than using the int function (as you mention)
my_input = int(my_input)
There is no shorter way than using the int function (as you mention)
Maybe you were hoping for something like my_number = my_input.to_int. But it is not currently possible to do it natively. And funny enough, if you want to extract the integer part from a float-like string, you have to convert to float first, and then to int. Or else you get ValueError: invalid literal for int().
The robust way:
my_input = int(float(my_input))
For example:
>>> nb = "88.8"
>>> int(nb)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 10: '88.8'
>>> int(float(nb))
88
how are you supposed to add an integer to a string?
Converting a string input to a number or integer
i am a beginner at python and im trying to convert string to int but it doesn't work
Convert string to Int. and parse the return.
im new to python and trying to learn how to print the sum of a integer and a string number, i get an error that says| unsupported operand type(s) +: 'int' and 'str' | i dont know how to solve it and everything i look up only has for int + int so im wondering if i have to just convert the string to a integer and if so how would that be done but if thats not what i need to do i would also really appreciate an explanation.
Hello Everyone,
I was wondering if it's possible to get the input of a string from the user and convert it into an integer later on. I'm asking this as the code I'm working on requires a string input from the user of what service they'd like. I later had to add up the totals of the services and wondered if once the choice was selected or inputted then if it would become a number that later adds up to the total cost if multiple services are inputted.
here is the code:
integer = 170
string = 'sadsad'
print(integer+int(string))
ps thx
x = 10
y = str(x)
print(y)
Why is 10.0 printed