inspection
str.isdigit
Returns True if every character in the string is a digit and the string is non-empty. Includes Unicode digit characters like superscripts (²) but excludes signs, decimal points, and fractions like ½.
str.isdigit() Parameters
| Parameter | Purpose |
|---|---|
| self | operates on the calling string; takes no arguments |
| returns | bool — False for empty strings |
Examples
>>> '12345'.isdigit()
True all ASCII digits
>>> '-1'.isdigit()
False sign character disqualifies
>>> '3.14'.isdigit()
False '.' is not a digit
>>> ''.isdigit()
False empty string is always False
Gotcha
Empty string returns False. For accepting integer input use `s.lstrip('-').isdigit()` or, safer, wrap `int(s)` in try/except.
Related methods
str.isalpha
Returns True if the string is non-empty and every character is an alphabetic Unicode letter. Digits, whitespace, and punctuation all return False.
str.isspace
Returns True if the string is non-empty and every character is whitespace. Whitespace includes spaces, tabs, newlines, and other Unicode whitespace characters.
str.strip
Returns a copy of the string with leading and trailing whitespace removed. If chars is given, removes any leading/trailing characters that appear in the chars set (not a prefix/suffix — it is a character set).