Reference Materials
Created with over a decade of experience.
Certification Courses
Created with over a decade of experience and thousands of feedback.
Python String Methods
- Python String capitalize()
- Python String center()
- Python String casefold()
- Python String count()
- Python String endswith()
- Python String expandtabs()
- Python String encode()
- Python String find()
- Python String format()
- Python String index()
- Python String isalnum()
- Python String isalpha()
- Python String isdecimal()
- Python String isdigit()
- Python String isidentifier()
- Python String islower()
- Python String isnumeric()
- Python String isprintable()
- Python String isspace()
- Python String istitle()
- Python String isupper()
- Python String join()
- Python String ljust()
- Python String rjust()
- Python String lower()
- Python String upper()
- Python String swapcase()
- Python String lstrip()
- Python String rstrip()
- Python String strip()
- Python String partition()
- Python String maketrans()
- Python String rpartition()
- Python String translate()
- Python String replace()
- Python String rfind()
- Python String rindex()
- Python String split()
- Python String rsplit()
- Python String splitlines()
- Python String startswith()
- Python String title()
- Python String zfill()
- Python String format_map()
Python String isdigit()
The isdigit() method returns True if all characters in a string are digits. If not, it returns False.
Example
str1 = '342'
print(str1.isdigit())
str2 = 'python'
print(str2.isdigit())
# Output: True
# False
Syntax of String isdigit()
The syntax of isdigit() is
string.isdigit()
isdigit() Parameters
The isdigit() doesn't take any parameters.
Return Value from isdigit()
The isdigit() returns:
- True if all characters in the string are digits.
- False if at least one character is not a digit.
Example 1: Working of isdigit()
s = "28212"
print(s.isdigit())
# contains alphabets and spaces
s = "Mo3 nicaG el l22er"
print(s.isdigit())
Output
True False
A digit is a character that has property value:
Numeric_Type = DigitNumeric_Type = Decimal
In Python, superscript and subscripts (usually written using unicode) are also considered digit characters. Hence, if the string contains these characters along with decimal characters, isdigit() returns True.
The roman numerals, currency numerators and fractions (usually written using unicode) are considered numeric characters but not digits. The isdigit() returns False if the string contains these characters.
To check whether a character is a numeric character or not, you can use isnumeric() method.
Example 2: String Containing digits and Numeric Characters
s = '23455'
print(s.isdigit())
#s = '²3455'
# subscript is a digit
s = '\u00B23455'
print(s.isdigit())
# s = '½'
# fraction is not a digit
s = '\u00BD'
print(s.isdigit())
Output
True True False
Also Read:
