Given a string and we have to check whether it contains only digits or not in Python.
To check that a string contains only digits (or a string has a number) – we can use isdigit() function, it returns true, if all characters of the string are digits.
Syntax:
string.isdigit()
Example:
Input:
str1 = "8789"
str2 = "Hello123"
str3 = "123Hello"
str4 = "123 456" #contains space
# function call
str1.isdigit()
str2.isdigit()
str3.isdigit()
str4.isdigit()
Output:
True
False
False
False
Python code to check whether a strings contains a number or not
Output
need an explanation for this answer? contact us directly to get an explanation for this answer