English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
If all characters in the string are numbers, the isdigit() method will return True. If not, it will return False.
The syntax of isdigit() is
string.isdigit()
isdigit() does not accept any parameters.
isdigit() returns:
True If all characters in the string are numbers.
False If at least one character is not a number.
s = "28212" print(s.isdigit()) # Contains letters and spaces s = "Mo3 nicaG ell22er" print(s.isdigit())
When running the program, the output is:
True False
Numbers are characters with property values:
Numeric_Type = number
Numeric_Type = decimal
In Python, superscripts and subscripts (usually written in unicode) are also considered numeric characters. Therefore, if the string contains these characters as well as decimal characters, then isdigit() returns True.
Roman numerals, currency denominations, and decimal points (usually written in unicode) are considered numeric characters, not numbers. If the string contains these characters, then isdigit() returns False.
To check if a character is a numeric character, you can use isnumeric()Method.
s = ''23455' print(s.isdigit()) # s = '²'3455' # Index is a number s = '\u00B'23455' print(s.isdigit()) # s = '½' # Fraction is not a number s = '\u00BD' print(s.isdigit())
When running the program, the output is:
True True False