Python String isdigit() Method

Python string isdigit() is a built-in function that checks whether the given string consists of only digits. The method returns True if the string has all its characters as digits and false otherwise.

Syntax

string.isdigit()

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 contains characters other than digits, like numerical or special characters. When it identifies a space, then also it returns false.

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

Example 1

string_variable = “1234”

print(string_variable.isdigit())

Output

True

Example 2

string = "12345"

print("It will print true as the string contains only digits:")

print(string.isdigit())

Output

It will print true as the string contains only digits:

True

That’s all!

Leave a Comment

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