Python int exception python isdigit

Source: Internet
Author: User
Tags python isdigit

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.