Methods and Functions

Python String isdigit()

Strings can hold any type of text, including numbers, alphabets, Roman numerals, and even hexadecimal values. But what if you needed to know if a string just included numbers? There might arise a situation where a user might want to see if a given input string for a password field contains a digit, ranging from 0-9. This issue can be solved by using the Python isdigit() method. Python includes a built-in checking method, isdigit(), that tells us if the string contains all digit values or not. Let us explore more about this method to better understand how it works and how it may be applied.

Python isdigit() function

Definition

  • Python isdigit() function returns a Boolean value TRUE if all the values in the input string are digits; else it returns FALSE.
  • The Python isdigit() string method is used to check if all the characters in the string are digit values or not. Digits are decimal number, exponents, subscripts, etc.

Python isdecimal() Syntax

The syntax for Python isdigit() function is as follows:

                    

string.isdigit()

isdigit() Parameters

Python isdigit() does not accept any parameters. If any argument is passed to the function, it will throw an error.

Return value from isdigit()

The Python string isdigit() function returns a Boolean value:

  • True – if all the characters in the input string are digits
  • False – if at least one of the characters in the string is not a digit

Note – If a string is empty, isdigit() function will always return a False value.

Example 1: Working of isdigit()

Python isdigit() returns True only if the string consists of all digit characters with no alphabets, no whitespaces, no special characters, no floating-point numbers, and no separators. Also, Roman numerals, monetary numerators, and fractions are not regarded as digits. As a result, the isdigit() function returns “False.”

Example

                    

# Python program to illustrate isdigit()
# string with all digits
s = '123789'
print(s.isdigit())

# string with digits and whitespaces
s = '123 567'
print(s.isdigit())

# string with alphabets and digits
s = 'Hello500'
print(s.isdigit())

# string with floating-point number
s = '10.42'
print(s.isdigit())

# string with separator
s = '10,20,500'
print(s.isdigit())

# empty string
s = ''
print(s.isdigit())

Output

                    

True
False
False
False
False
False

Example 2: String containing Digits and Numeric characters

Superscripts and subscripts are treated as Digit characters rather than Decimal characters. Python isdigit() returns True if the string contains these characters (which are generally written in Unicode).

Roman numerals, currency numerators, and fractions are all considered numeric numbers (typically represented in Unicode), but digits are not. In this situation, the isdigit() function returns False.

Example

                    

# Python program to illustrate isdigit()
# string with superscript
s = '10\u00B2'
print('Original string:', s)
print('Is decimal?:', s.isdigit())

# string with subscript
s = '10\u2082'
print('Original string:', s)
print('Is decimal?:', s.isdigit())

# string with fraction
s = '\u2156'
print('Original string:', s)
print('Is decimal?:', s.isdigit())

Output

                    

Original string: 10²
Is decimal?: True

Original string: 10₂
Is decimal?: True

Original string: ⅖
Is decimal?: False

Example 3: Special Unicode Category

If a character may be used to make a number in base 10, under Unicode General Category ‘Nd’, it is classified as a digit character. For example, ? (U0001D7DC, MATHEMATICAL DOUBLE-STRUCK DIGIT FOUR) and (U+0660, ARABIC-INDIC DIGIT ZERO) are also considered as digits.

Example

                    

s = '\u0662'
print('Original value in Arabic:', s)
print('Decimal Value:', int(s))
print(s.isdigit())

s = '\u0668'
print('Original value in Arabic:', s)
print('Decimal Value:', int(s))
print(s.isdigit())

s = '\U0001D7DC'
print('Original value:', s)
print('Decimal Value:', int(s))
print(s.isdigit())

Output

                    

Original value in Arabic: ٢
Decimal Value: 2
True

Original value in Arabic: ٨
Decimal Value: 8
True

Original value: 𝟜
Decimal Value: 4
True

Frequently Asked Questions

Q1. What is isdigit in Python?

The Python String isdigit() method is a built-in string handling method. If all of the characters in the string are digits, the isdigit() method returns “True.” Otherwise, it returns “False.” This function determines whether the argument contains digits such as 0123456789. The isdigit() method accepts only decimals, subscripts, and superscripts.

Syntax:

                    

string.isdigit()

Example

                    

val = '100'
print(val.isdigit())

val = '\u00b255'   # superscript 2
print(val.isdigit())

# alphabets with digits
val = 'H3ll0 R0ss'
print(val.isdigit())

# fraction
val = '½'
print(val.isdigit())

Output

                    

True
True
False
False

Q2. What is the difference between isdigit and isnumeric in Python?

  • Python isdigit() – If all of the characters in a string are digits, the Python isdigit() method returns True. If it does not, it returns False. The Python String isdigit() method is a built-in method for string manipulation and handling.
  • Python isnumeric() – The Python isnumeric() String function examines a string for numeric characters and returns True only if the string contains all numeric characters, i.e. numerals (0-9); else returns False.

Example

                    

val = '50'
print(val.isdigit())
print(val.isnumeric())
print('')
 
# u00b2 is Superscript two
val = '\u00b2'
print(val.isdigit())
print(val.isnumeric())
print('')

val = '⅓'
print(val.isdigit())
print(val.isnumeric())

Output

                    

True
True

True
True

False
True

The main difference between both the functions is:

  • The isdigit() method accepts only decimals, subscripts, and superscripts.
  • The isnumeric() function supports Digits, Vulgar Fractions, Subscripts, Superscripts, Roman Numerals, and Currency Numerators.

Q3. How does isdigit work in C++?

In C++, the isdigit() function determines whether or not a given character is a digit [0-9]. The cctype header file defines it. The syntax is as follows:

where, ch – character to be checked

If the character is a digit, the function returns a non-zero value (True) (1); else it returns (False) (0).

Example

                    

#include <iostream>
using namespace std;

int main() {
  cout << isdigit('9') << endl;
  cout << isdigit('23.64');
  return 0;
}

Output

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.