Python int is a way for Python to convert any type to int, but if you use it badly it throws an exception, but Python's str string conversion method is safe to use, and it does not report an exception when converting any object to a string type. Now give an example of Python int: for example a = ' 123 ' b = Int (a) The result of print B is 123 a = ' abc ' b = Int (a) print B, it will be reported: Valueerror:invalid literal fo R Int () with base: ' Fee ' again gives an example of Python str: while a = ' 123 ' B = str (a) The result of print B is 123 a = ' abc ' b = str (b) The result of print B is: ABC All from the above example can tell us to use the Int method to pay special attention, otherwise it is easy to cause an exception. If there are problems to be solved, what methods can be solved well? I say I use the 2 methods, one is to directly catch the exception, but first to determine whether it is a character, if it is a character, do not call the method of int. 1., directly catch the exception: Try:a = ' AaB ' b = Int (a) except:b = ' 123 ' 2, is the first to determine whether it is a character, if it is a character, do not call the method of int: a = ' abc ' If A.isdigit (): b = Int (a) else:b = ' 123 '
Python int exception python isdigit