The implementation code in Python to determine if the input is a number

  • 2020-09-16 07:35:36
  • OfStack

After you receive the raw_input method, determine if the received string is a number

Such as:


str = raw_input("please input the number:")
if str.isdigit():

For True, all characters entered are Numbers; otherwise, not all are Numbers

str is the string str.isalnum () all characters are Numbers or letters str.isalpha () all characters are letters str.isdigit () all characters are Numbers str.islower () all characters are lowercase str.isupper () all characters are uppercase str.istitle () all words are uppercase, Like the title str.isspace () all characters are white space characters, \t, \n, \r
The mainly for integer Numbers above, but is not applicable for floating point Numbers, so how to judge, floating point 1 straight struggling with this question, why have to distinguish between integer and floating point Numbers, now that are involved in the operation, all applicable floating point number 1 sample, isn't it, after getting the results, directly into int type 1 sample, isn't it, why entanglements in the early to judge whether an integer or floating point Numbers, with such a train of thought, here it is good to do, such as:

We can judge by exceptions. The exception syntax is as follows:

try: {statements} exception: {Exception Objects} {statements}

str = raw_input("please input the number:")
try: f = float(str) exception ValueError: print(" Not a number entered!" )

There is also a pure way to determine whether it is a floating point number, using regular expressions:

1.# references the re regular module


import re
float_number = str(input("Please input the number:"))
# Call the regular 
value = re.compile(r'^[-+]?[0-9]+\.[0-9]+$')
result = value.match(float_number)
if result:
  print "Number is a float."
else:
  print "Number is not a float." 

2. For this regular expression, explain 1 as follows:

[

^[-+]?[0-9]+\.[0-9]+$

]

^ means beginning with this character, that is, beginning with [-+], [-+] means character - or + 1,
The & # 63; It means zero or one, which means the symbol is optional.
Similarly, [0-9] represents 1 number from 0 to 9, and + represents 1 or more, which is the integer part.
\. Represents the decimal point, \ is an escape character because. Is a special symbol (matches any single character other than \r\n),
So you need to escape.
By the same token, the $represents the end of the string.

Determine if input is all Numbers, characters, etc

s is a string

All characters are numeric or alphanumeric, return Ture for true, otherwise return False.
s.isalpha() All characters are letters, return Ture for true, otherwise return False.
All characters are Numbers, return Ture for true, otherwise return False.
s. islower() all characters are lowercase and return Ture for true, otherwise return False.
s.isupper() all characters are uppercase and return Ture for true, otherwise return False.
s. istitle() All words are capitalized, return Ture for true, otherwise return False.
s. isspace() All characters are white space characters, return Ture for true, otherwise return False.

Such as:


s = 'I LOVE YOU' 
s.isupper() 
True

Related articles: