isdigit() function for string in Python

In this tutorial, we will learn how to use isdigit() function in Python. The right way to use this in our Python program.

In many programming problems, we have to check whether a given string is an integer or not. To do this we can use ASCII value to find them but it is a too long process. Python provides us a simple method to achieve this. Let’s see what is it.

isdigit() method in Python

isdigit() is the pre-defined function in python to check whether the given string is fully integer or not. If that string contains a single alphabet then this function will return “False” otherwise “True”.

Way to use isdigit() in Python

isdigit() doesn’t take any arguments so if you pass any arguments through the parameters then it will through an error. So use dot operator after the string variable to use it. For example

#Correct Way

myString = '98765'
print(myString.isdigit())

myString = '987ag65' 
print(myString.isdigit())

Output:

True

False

#Wrong Way

myString = '98765' 
print(isdigit(myString))

Output:

NameError: name ‘isdigit’ is not defined

You may also learn:

Leave a Reply

Your email address will not be published. Required fields are marked *