How to Use String isdigit() Method in PythonPython Basics


The isdigit() method checks if a string consists of only numbers. It means that a string will be judged to be not digital if any part is not a number.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

str1 = '123456789'
str2 = '1234567.89'
str3 = "12346+789"

print(str1.isdigit())
print(str2.isdigit())
print(str3.isdigit())

Output:

True
False
False

Here ‘1234567.89’ is still judged as not digital because the dot ‘.’ is not number.

Syntax

str.isdigit()

Parameters

The method doesn’t take any parameters.

Return Value

It returns boolean value (True or False).

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us