Python's method of determining whether a string is a pure number _python

Source: Internet
Author: User

The example in this article describes how Python determines whether strings are pure digits. Share to everyone for your reference. Specifically as follows:

Judged by the code below, through the anomaly can not distinguish between the positive and negative signs, regular expressions can be more flexible according to their needs to write, through the IsDigit method to determine whether it is pure numbers, test code as follows

Copy Code code as follows:
#!/usr/bin/python
#-*-coding:utf-8-*-
A = "1"
B = "1.2"
c = "A"
#通过抛出异常
def is_num_by_except (num):
    try:
     & nbsp;  int (num)
        return True
    except Valueer ROR:
#        print '%s valueerror '% num
     &N bsp;  return False
Print by throwing Exceptions
print "A", Is_num_by_except (a)    
print "B", Is_n Um_by_except (b)
print "C", Is_num_by_except (c)
print "Through IsDigit ()"
print "a", A.isdigit ()
print "B" ", B.isdigit ()
print" C ", C.isdigit ()
print" Through regular expression "
Import re
print" A ", Re.match (R" d+$ ", a) and True or false
print "B", Re.match (R "d+$", B) and true or false
print "C", Re.match (R "d+$", c) and True or Fal Se

The output results are as follows:
Copy Code code as follows:
By throwing an exception
A True
b False
C False
Through IsDigit ()
A True
b False
C False
Through regular expressions
A True
b False
C False
--eof--

To determine that a string contains only numeric characters

One method is A.isdigit (). However, this method is not valid for numeric strings that contain positive and negative numbers, so it is more accurate:

Copy Code code as follows:
Try
x = Int (apossibleint)
... do something with x ...
Except ValueError:
... do something else ...

This is more accurate and more adaptable. But if you are convinced that there is no sign, it is more convenient to use the IsDigit () method of the string.
You can also use regular expressions:
Copy Code code as follows:
Re.match (R ' [+-]?d+$ ', ' -1234′ ')

When the number is large, it may be faster than converting with type int.

I hope this article will help you with your Python programming.

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.