Converting Float entered as String to "int" datatype in Python


I learnt today that “int” really takes only integers and not floats. In other languages that am aware int takes float but returns the integer value only if its a float. For ex int(2.3)=2. Looks like that does not work in Python. This means when we get numeric data as string and convert it, maybe its a good idea to always use “float” instead of “int”


It worked in python 3. check

Hello @hari.yajurveda

The statement you made is not clear; Could you explain in more detail.

In python int(2.3) = 2 works, if that is what you are pointing out.

Again could explain the context of this;

Am I missing any important point that you are trying to make?

Even it works in Python2

Thank you for the response. Lets look at this line
hours_per_day = input(“How many hours a day do you study”)
print(“I study " + str(int(hours_per_day)*7) + " hours in a week”)

When I input “2” for hours_per_day, I get the required output, but when I input 2.5, I get an error. I expected that “int” function takes only the integer part and still print the same as when supplied a value “2”

Am I not understanding something correctly ?


I worked on this and established following understanding on the basis of my work -
i am using python 3.
i created one list as given below [Float values as String]:
myintlist = ['2.2','3.9','5.6','6.7']
and then itrate it and print

for memner in myintlist :

it will print string then i try to convert in integer:

for member in myintlist :

Traceback (most recent call last):
File “<pyshell#5>”, line 2, in
ValueError: invalid literal for int() with base 10: ‘2.2’

but when i try to convert in flot then it will work:

for member in myintlist :


so aftert that first i convert str to float and then float to int then it work as expected:

for member in myintlist :


Conclusion: If you want to convert any string into int and you are not sure about its value then i suggest you first convert that string into float type and then convert this float type to int type.
str > float > int

Thank you. That was my conclusion too as in the last line in my email to always use “float” first