Q.what happens when you try to convert a string into an integer but the string is not a number?
Answers
First, I would want to star off by explaining what strings and integers are.
____________________________________________
STRINGS
A string is a list characters. These characters could be anything raging from alphabets, numbers to all the ancient characters, foreign languages.In short, these are called as Unicode characters. But, one and only way to identify a string is either with the ' ' or " ".
INTEGERS
Integers are basically numbers. 1 to infinity. It has no limit.
____________________________________________
One type can be converted to an other type.
int - integer
str - string
float - float (decimal numbers)
____________________________________________
If we would want an integer or a float to be a string, we just add str() before it.
23 - integer
str(23) = '23' - String
String could be converted to integers too but only when there are numbers in the strings.
int('23') - convertible
int('234567') - convertible
int('number') - not convertible
When it is not convertible, there be an error raised.
Traceback (most recent call last):
File "<pyshell#27>", line 1, in <module>
int('number')
ValueError: invalid literal for int() with base 10: 'number'
It is a ValueError because all these int, str, floats are values and when it is not compatible, there is an error.