English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
If all the 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 the 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él22er" 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, isdigit() returns True.
Roman numerals, currency denominators, and decimals (usually written in unicode) are considered numeric characters rather than numbers. If the string contains these characters, isdigit() returns False.
To check if a character is a numeric character, you can use isnumeric()Method.
s = ''23455' print(s.isdigit()) # s = '²'3455' # The index is a number s = '\u00B'23455' print(s.isdigit()) # s = '\u00B' # The fraction is not a number s = '\u00BD' print(s.isdigit())
When running the program, the output is:
True True False