0

Is string variable convert into integer or float?

A = "55" B = int(A) Print(B) Output: 55 But , A= "Ram" B= int(A) Print(B) Output: Error

2nd Dec 2021, 4:09 AM
Chintala Santhosh
10 Respostas
+ 2
Mention the programming language also, cause every language behave differently.
2nd Dec 2021, 4:32 AM
zexu knub
zexu knub - avatar
+ 2
From the output this is obvious that Python can't convert a string that contains letters to an integer. If you have numbers in string then it will work. and of course it will convert it to integer not the float. for that you need to use - float(B) while using int() for float value will only give error. A = '12.0' A = int(A) print(A) # error because we have float value as a string
2nd Dec 2021, 4:42 AM
zexu knub
zexu knub - avatar
+ 1
Chintala Santhosh Char, Int & Float are represented differently in memory & act differently in a program. Every programming languages has its own way of handling this. https://www.quora.com/What-is-the-difference-between-int-char-and-float
2nd Dec 2021, 5:03 AM
zexu knub
zexu knub - avatar
+ 1
Okay thank you I will check with your link
2nd Dec 2021, 5:04 AM
Chintala Santhosh
+ 1
Chintala Santhosh Sure, good luck 👌
2nd Dec 2021, 5:06 AM
zexu knub
zexu knub - avatar
+ 1
Hey, yes this is python. And actually the problem isn't so much in how you coded it, but that int will only work on whole numbers. Hope this helps.
2nd Dec 2021, 8:25 PM
Joseph Fruin
Joseph Fruin - avatar
2nd Dec 2021, 4:33 AM
Chintala Santhosh
0
Thank sir thank you so much
2nd Dec 2021, 4:52 AM
Chintala Santhosh
0
Actually my question is Why numbers not characters .... Is Python works like this only
2nd Dec 2021, 4:55 AM
Chintala Santhosh
0
I am new to programs this is my first program don't hesitate with my questions
2nd Dec 2021, 4:57 AM
Chintala Santhosh