Python

Python isdigit() function

Python has many built-in functions to check the validity of the data. The isdigit() function is one of them. It is used to check the characters of a string are all digits or not. The different uses of this function have shown in this tutorial.

Syntax:

The syntax of this function has given below.

bool string.isdigit()

The function does not argue. It returns a Boolean value. If the value of the string contains all digits, then it returns True. It returns False for all alphabetic and special characters, decimal point, plus sign, or minus sign.

Different uses of isdigit() function:

The uses of the isdigit() function have been shown by using multiple examples in this part of the tutorials.

Example-1: Checking different types of string values using the isdigit() function

Create a python script with the following script to check the return value of the isdigit() function for different types of string values. The first string variable has been defined by the single quote that contains all digits. The second string variable has been defined by the double quote containing all digits. The third-string variable contains the digits with the hyphen (-). The fourth string variable contains alphabetic characters.

# Declare a string of numbers with single quotes
number1 = '981'
# Declare a string of numbers with double quotes
number2 = "563"
# Declare a string with numbers and '-'
number3 = "880-19378-38978"
# Declare a string of alphabetic characters
number4 = 'Two'

# Check and print the variables are numbers or not
print("The {} is valid number : {}".format(number1, number1.isdigit()))
print("The {} is valid number : {}".format(number2, number2.isdigit()))
print("The {} is valid number : {}".format(number3, number3.isdigit()))
print("The {} is valid number : {}".format(number4, number4.isdigit()))

Output:

The following output will appear after executing the script.

Example-2: Checking digits of different formats using the isdigit() function

Create a python file with the following script to check the Unicode of different types of numbers and the Arabic digit. The Unicode of the 3² has been checked by the first variable. The Unicode of ¼ has been checked by using the second variable. The Unicode of the digit, 5, has been checked by the third variable. The fourth variable has checked the character value of Arabic digit 3.

# Checking the Unicode of 3²
number1 = '3\u00B2'
print("The 3² is number: ", number1.isdigit())

# Checking the Unicode of fractional value
number2 = '\u00BC'
print("The ¼ is number: ", number2.isdigit())

# Checking the Unicode of number 5
number3 = '\u0035'
print("The {} is number: {}".format(number3, number3.isdigit()))

# Checking the Arabic digit 3
number4 = '٣'
print("The ٣ is number: ", number4.isdigit())

Output:

The following output will appear after executing the script.

Example-3: Validating data using isdigit() function

Create a python file with the following script to check the input data are valid or not and print the error message for invalid data. Two input values will be taken from the user, and the isdigit() function will be used to check the value of the book_name and book_price variables. If the value of the book_name contains any digit, then an error message will be printed. If the value of the book_price contains any non-digit character, then an error message will be printed.

# Take book name and price from the user
book_name = input("Enter the book name:")
book_price = input("Enter the book price:")

# Set the error flag
error = False
# Check the value of the book name is valid or not
if int(book_name.isdigit()) == True:
print("Book name can contain character only.")
    error = True
# Check the value of the book price is valid or not
if book_price.isdigit() == False:
print("Book price can contain digits only.")
    error = True
# Print the input values if no error exists
if error == False:
print("Book name:", book_name)
print("Book price:", book_price)

Output:

The following output will appear after executing the script for the input values, ‘Learning Laravel’ and 50.

The following output will appear after executing the script for the input values, ‘Learning Python’ and ‘Fifty’.

The following output will appear after executing the script for the input values, 600 and 60.

Conclusion:

The isdigit() function has been used to check different types of string values in the examples of this tutorial. This function is mainly used for checking data validity.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.