Python String isdigit() Method

Python string isdigit() method returns True if all characters in the string are digits; otherwise, returns False.

Syntax

string.isdigit()

Parameters

The function doesn’t take any parameters. There will error shown if we try to pass any parameter to the method.

Return value

It returns True if the string consists of only digits, 

It returns False if the string doesn’t contain digits or characters other than digits, like numerical or special characters. When it identifies a space, also it returns false.

We can use the ascii values of characters and print the digits using isdigit().

Visual RepresentationVisual Representation of How to Use String isdigit() function

Example 1: How to Use String isdigit() Function

string_variable = “1234”

print(string_variable.isdigit())

Output

True

Example 2: String Containing digits and Numeric CharactersVisual Representation of String Containing digits and Numeric Characters

s1 = '$2100'
print(s1.isdigit())

s2 = '1900 '
print(s2.isdigit())

num = '\u0038'
print(num.isdigit())

mystr = '\u00380061'
print(mystr.isdigit())

arabic_six = '۶'
print(arabic_six.isdigit())

Output

False
False
True
True
True

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.