There might arise a situation where a user might want to see if a given input string contains a number, specifically a decimal number. Or we might want to accept a user input containing only Decimal characters and values. These problems can be solved by using the Python isdecimal() method. Python includes a built-in checking method, isdecimal(), that tells us if the string contains all decimal values or not. Let us explore more about the Python isdecimal() string method to better understand how it works and how it may be applied.
Definition
- Python isdecimal() function returns a Boolean value TRUE if the input string contains all decimal characters; else it returns FALSE.
- The Python isdecimal() string method is used to check if all the characters in the string are decimal values or not. Decimal characters are numbers with a base 10.
Python isdecimal() Syntax
The syntax for Python isdecimal() function is as follows:
string.isdecimal()
isdecimal() Parameters
Python isdecimal() does not accept any parameters. If any argument is passed to the function, it will throw an error.
Return value from isdecimal()
The Python string isdecimal() function returns a Boolean value:
- True – if all the characters in the input string are decimal values
- False – if at least one of the characters in the string is not a decimal character
Note – If a string is empty, isdecimal() function will always return a False value.
Example 1: Working of isdecimal()
Python isdecimal() returns True only if the string consists of all decimal characters with no alphabets, no whitespaces, no special characters, no floating-point numbers, and no separators. Let us look at a few use-cases to understand this.
Example
# Python program to illustrate isdecimal()
# string with all decimal characters
s = '123789'
print(s.isdecimal())
# string with decimal characters and whitespaces
s = '123 567'
print(s.isdecimal())
# string with alphabets and decimals
s = 'Hello500'
print(s.isdecimal())
# string with floating-point number
s = '10.42'
print(s.isdecimal())
# string with separator
s = '10,20,500'
print(s.isdecimal())
# empty string
s = ''
print(s.isdecimal())
Output
True
False
False
False
False
False
Example 2: String containing Digits and Numeric characters
Superscripts and subscripts are treated as Digit characters rather than Decimal characters. Python isdecimal() returns False if the string contains these characters (which are generally written in Unicode).
Similarly, roman numerals, currency numerators, and fractions are all considered numeric numbers (typically represented in Unicode), but decimals are not. In this situation, the isdecimal() function also returns False.
Example
# Python program to illustrate isdecimal()
# string with superscript
s = '10\u00B2'
print('Original string:', s)
print('Is decimal?:', s.isdecimal())
# string with subscript
s = '10\u2082'
print('Original string:', s)
print('Is decimal?:', s.isdecimal())
# string with fraction
s = '\u2156'
print('Original string:', s)
print('Is decimal?:', s.isdecimal())
Output
Original string: 10²
Is decimal?: False
Original string: 10₂
Is decimal?: False
Original string: ⅖
Is decimal?: False
Example 3: Special Unicode Category
If a character may be used to make a number in base 10, under Unicode General Category ‘Nd’, it is classified as a decimal character. For example, ? (U0001D7DC, MATHEMATICAL DOUBLE-STRUCK DIGIT FOUR) and (U+0660, ARABIC-INDIC DIGIT ZERO) are also considered as decimal characters.
Example
s = '\u0662'
print('Original value in Arabic:', s)
print('Decimal Value:', int(s))
print(s.isdecimal())
s = '\u0668'
print('Original value in Arabic:', s)
print('Decimal Value:', int(s))
print(s.isdecimal())
s = '\U0001D7DC'
print('Original value:', s)
print('Decimal Value:', int(s))
print(s.isdecimal())
Output
Original value in Arabic: ٢
Decimal Value: 2
True
Original value in Arabic: ٨
Decimal Value: 8
True
Original value: 𝟜
Decimal Value: 4
True
Frequently Asked Questions
Q1. What is isdecimal in Python?
Python includes a built-in checking method, isdecimal(), that tells us if the string contains all decimal values or not. The Python isdecimal() string method is used to check if all the characters in the string are decimal values or not. Decimal characters are numbers with a base 10. The isdecimal() function returns True only if the string consists of all decimal characters with no alphabets, no whitespaces, no special characters, no floating-point numbers, and no separators.
Example
my_str = '9999'
print(my_str.isdecimal())
my_str = '20 20'
print(my_str.isdecimal())
my_str = '90,900'
print(my_str.isdecimal())
my_str = 'Hello2021'
print(my_str.isdecimal())
my_str = '25.693'
print(my_str.isdecimal())
Output
True
False
False
False
False
Q2. What is the difference between isdigit and isdecimal in Python?
- Python isdigit() – If all of the characters in a string are digits, the Python isdigit() method returns True. If it does not, it returns False. The isdigit() method accepts only decimals, subscripts, and superscripts.
- Python isdecimal() – If all of the characters in a string are decimal characters, this function returns True, else it returns False. The isdecimal() method accepts only decimals.
Example
val = '50'
print(val.isdigit())
print(val.isdecimal())
print('')
val = '⅓'
print(val.isdigit())
print(val.isdecimal())
print('')
val = '\u00b2' # superscript 2
print(val.isdigit())
print(val.isdecimal())
Output
True
True
False
False
True
False
Q3. How do I use isdigit in Python?
The Python String isdigit() method is a built-in string handling method. If all of the characters in the string are digits, the isdigit() method returns “True.” Otherwise, it returns “False.” This function determines whether the argument contains digits such as 0123456789. The isdigit() method accepts only decimals, subscripts, and superscripts.
The Syntax for Python isdigit() is as follows:
string.isdigit()
Example
val = '100'
print(val.isdigit())
val = '\u00b255' # superscript 2
print(val.isdigit())
# alphabets with digits
val = 'H3ll0 R0ss'
print(val.isdigit())
# fraction
val = '½'
print(val.isdigit())
Output
True
True
False
False
Leave a Reply