Python String isdigit() Method - GeeksforGeeks

12 Aug, 2021
Python String isdigit() method is a built-in method used for string handling. The isdigit() method returns “True” if all characters in the string are digits, Otherwise, It returns “False”. This function is used to check if the argument contains digits such as 0123456789
Syntax:
string.isdigit()
Parameters:
isdigit() does not take any parameters
Returns:
True – If all characters in the string are digits.
False – If the string contains 1 or more non-digits.
Errors And Exceptions:  
It does not take any arguments, therefore it returns an error if a parameter is passedSuperscript and subscripts are considered digit characters along with decimal characters, therefore, isdigit() returns “True”.The Roman numerals, currency numerators, and fractions are not considered to be digits. Therefore, the isdigit() returns “False”
It does not take any arguments, therefore it returns an error if a parameter is passed
Superscript and subscripts are considered digit characters along with decimal characters, therefore, isdigit() returns “True”.
The Roman numerals, currency numerators, and fractions are not considered to be digits. Therefore, the isdigit() returns “False”
Input : string = '15460'
Output : True

Input : string = '154ayush60'
Output : False
Python3
# Python code for implementation of isdigit()   # checking for digitstring = '15460'print(string.isdigit())   string = '154ayush60'print(string.isdigit())
Output: 
True
False
Application: Using ASCII values of characters, count and print all the digits using isdigit() function. 
Algorithm: 
Initialize a new string and a variable count=0.Traverse every character using ASCII value, check if the character is a digit. If it is a digit, increment the count by 1 and add it to the new string, else traverse to the next character. Print the value of the counter and the new string. 
Initialize a new string and a variable count=0.
Traverse every character using ASCII value, check if the character is a digit. 
If it is a digit, increment the count by 1 and add it to the new string, else traverse to the next character. 
Print the value of the counter and the new string. 
Python3
# Python program to illustrate # application of isdigit()# initialising Empty stringnewstring =''  # Initialising the counters to 0count = 0  # Incrementing the counter if a digit is found # and adding the digit to a new string# Finally printing the count and the new string  for a in range(53):    b = chr(a)    if b.isdigit() == True:        count+= 1        newstring+= b          print("Total digits in range :", count)print("Digits :", newstring)
Output: 
Total digits in range : 5
Digits : 01234
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.
String Containing digits and Numeric Characters
Python3
s = '23455'print(s.isdigit())  # s = '23455'# subscript is a digits = '\u00B23455'  print(s.isdigit())  # s = '1⁄2'# fraction is not a digits = '\u00BD'  print(s.isdigit())
Output:
True
True
False
mohammadnaved1351
AmiyaRanjanRout
Python-Built-in-functions
python-string
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Read JSON file using Python
Adding new column to existing DataFrame in Pandas
Python map() function
How to get column names in Pandas dataframe
Python Dictionary
Taking input in Python
Read a file line by line in Python
How to Install PIP on Windows ?
Enumerate() in Python
Different ways to create Pandas Dataframe