Python str.isdigit() function
In Python, the str.isdigit()
method is used to check if all characters in a string are digits. This method returns True
if all characters in the string are numeric characters (i.e., 0 through 9) and there is at least one character; otherwise, it returns False
.
Syntax
Example Usage
- Basic usage with digits:
- Including non-digit characters:
- Checking an empty string:
- With spaces and punctuation:
- Using special numeric characters:
The isdigit()
method also returns True
for characters that are considered numeric in Unicode, such as fractions and superscripts:
- Negative numbers and decimals:
Important Notes
- The
isdigit()
method considers characters that are numeric, including those in various scripts (like Arabic numerals) and even other characters defined as digits in Unicode. - It will return
False
for any string containing non-numeric characters, including letters, spaces, punctuation, and symbols.
Summary
- Use
str.isdigit()
to determine if a string consists entirely of digit characters. - It returns
True
for strings made up only of digits (0-9) andFalse
for any strings containing non-digit characters.