In Python, isdigit() 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
string.isdigit() Parameters: isdigit() does not take any parameters Returns : 1.True- If all characters in the string are digits. 2.False- If the string contains 1 or more non-digits.
Input : string = '15460' Output : True Input : string = '154ayush60' Output : False
Errors And Exceptions
- 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”
Application : Using ascii values of characters, count and print all the digits using isdigit() function.
1. Initialize a new string and a variable count=0 .
1. Traverse every character using ASCII value, check if the character is a digit.
2. If it is a digit, increment the count by 1 and add it to the new string, else traverse to the next character.
3. Print the value of the counter and the new string.
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
True True False
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- numpy string operations | isdigit() function
- Python | Pandas Series.str.isdigit()
- Python String isspace() and its application
- Python String isalpha() and its application
- Python String isnumeric() and its application
- Build an Application to Search Installed Application using Python
- divmod() in Python and its application
- isprintable() in Python and its application
- Login Application and Validating info using Kivy GUI and Pandas in Python
- Profile Application using Python Flask and MySQL
- Build an Application to extract URL and Metadata from a PDF using Python
- Python String index() and its applications
- Birthday Reminder Application in Python
- Python | Create simple animation for console-based application
- English dictionary application using Python
- Python | ToDo GUI Application using Tkinter
- Hierarchical treeview in Python GUI application
- Create First GUI Application using Python-Tkinter
- GUI chat application using Tkinter in Python
- Python VLC Instance - Setting Application Name
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : mohammadnaved1351