. Advertisement .
..3..
. Advertisement .
..4..
How to check if a character is a number in Python? This article will provide four methods to deal with this case.
How To Check If A Character Is A Number In Python
Method 1: Use Python String isdigit()
This method will return True if all characters in a string are digits. On the reverse, the result returns False.
Input:
example_string1 = '260822'
print(example_string1.isdigit())
example_string2 = 'python'
print(example_string2.isdigit())
Output:
False
True
For example:
Input:
string3 = "280315"
print(string3.isdigit())
string4 = "Check If A3 Character Is A Number In Python10"
print(string4.isdigit())
Output:
True
False
Method 2: Use Python String isnumeric() method
The isnumeric() method will offer you a True result if all the characters are numeric, ranging from 0 to 9, otherwise False.
Such exponents as ² and ¾ are also considered to be numeric in Python. On the other hand, negative values like “-5” and “3.8” are NOT considered numeric.
Overall, values with the – and the dot in the string are not numeric.
For example:
Input:
str1 = "\u0030" #unicode for 0
str2 = "\u00B2" #unicode for ²
str3 = "128km2"
str4 = "-5"
str5 = "3.8"
print(str1.isnumeric())
print(str2.isnumeric())
print(str3.isnumeric())
print(str4.isnumeric())
print(str5.isnumeric())
Output:
True
True
False
False
False
Method 3: Use The If-Else Conditional Statement
The If-else conditional statement is one of the easiest and most simple methods to check if your character is a number.
Input:
char = input("Enter the character that you want to check: ")
if(char >= '0' and char <= '9'):
print("It is a Number")
else:
print("It isn't a Number")
Output:
Enter The character that you want to check: 283228
It is a Number
Method 4:Use ASCII Values
ASCII stands for American Standard Code for Information Interchange. This standard can assign letters, numbers, and any characters in an 8-bit code.
To find a character’s ASCII value, use the ord() function and follow the unicode code point:
Input:
ex_char = input("Enter the character: ")
print("The ASCII value of character " + ex_char + " is: ",ord(ex_char))
Output:
Enter the character: F
The ASCII value of character F is: 70
Every character has a unique ASCII value, and you must use the if-else conditional statement to check the ASCII values. The numbers 0 to 9 will have the ASCII value 48 to 57. So we have the following code:
Input:
check_char = input("Enter the character that you want to check: ")
if(ord(check_char) >= 48 and ord(check_char) <= 57):
print("It is a Number")
else:
print("It isn't a Number")
Output:
Enter the character that you want to check: 9
It is a Number
How To Check If A String Contains A Number In Python
To check if a string contains a number, the str.isdigit() function is the best choice.
Input:
contains_digit = False
ex_char1 = "python3.10"
for character1 in ex_char1:
if character1.isdigit():
contains_digit = True
if contains_digit == True:
print('The string contains the number')
else:
print('The string does not contain the number')
Output:
The string contain the number
Conclusion
A string can store numeric values belonging to double quotes with digits between 0 and 9. The isdigit() and the isnumeric() functions work in the same process and give the same output.
However, while the former one returns True for digits, the latter returns True for any value containing numeric characters. Therefore, choose a suitable method.
Leave a comment