Python Practice › Loop Programs › Program 58
Does a string contain a digit?
Write a Python program to check whether a string contains a digit.
Input
"Python123", "Python"
Output
Python123 True Python False
Approach
- Loop until a digit is found; use
for ... elsefor the “not found” case.
Solution
for text in ["Python123", "Python"]: for ch in text: if ch.isdigit(): print(text, True) break else: print(text, False)Output
Python123 True Python False
Line by line
Line 5
break stops at the first digit.
Lines 6-7
The else runs only when the loop ended without finding one.
Learn the concept
New to this topic? Read the lesson, then come back.