isdigit() in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

Python isdigit() function is an inbuilt method for string handling. It is used to check whether all the string characters are digits(from 0 to 9) or not. This function doesn't take any parameters. This function is used on strings and returns a boolean value based on conditions.

Syntax of isdigit() function in python

The syntax for isdigit() function in Python:

Parameter of isdigit() function in python

This function doesn't take any parameters.

Return Value of isdigit() function in python

Return Type: <bool>

This function returns the boolean value based on the situation. isDigit() returns True if all the string characters are digits. Else it returns False.

If the string is empty, it means that string has no digit inside it, so it will also return False.

Exceptions of isdigit() function in python

  • As isdigit() does not take any arguments. Therefore it raises an error if a parameter is passed.
  • Roman numbers and fractions are not considered digits, so that it will return False if this function is used on these kinds of strings.

Example of isdigit() function in python

As discussed in the above section, check whether the phone number entered by the employee is correct or not. Now, let's implement that part to understand more about isDigit().

Code:

Output:

What is isDigit() function in python?

Let's think of a situation where you have created a form for taking the phone number of the employees. Now, you have to check whether the entered number is a valid number or not. isDigit() function of python is the best way to handle this problem. isDigit() is an inbuilt function of python which is used to check whether all characters of strings are digit or not. Read along to know more about the working of isDigit(). What is isDigit in python

More Examples

Example 1: Working of isDigit()

As we understood above, isdigit() does not take any parameter. It is just used with string to check whether every character of the string is a digit.

Here we will write the code to check if the pin code entered by the user is valid or not.

Code:

Output:

Working of isDigit() output

Example 2: String Containing digits and Numeric Characters

Here we will write a code to check isdigit() with different types of strings. String Containing digits and Numeric Characters

String Containing digits and Numeric Characters code output

String Containing digits and Numeric Characters code output 2

String Containing digits and Numeric Characters code output 3 Code:

Output:

Conclusion

  • isDigit(), the inbuilt function of python is used to check whether each and every character of the string is a digit or not.
  • isDigit() doesn't takes any parameter.
  • It returns a true value only if all the string characters are digits. Else, in every other condition, it returns false.