Python Practice › Loop Programs › Program 58

Does a string contain a digit?

EasyLoop Programs · 58 of 90

Write a Python program to check whether a string contains a digit.

Advertisement
Input
"Python123", "Python"
Output
Python123 True
Python False

Approach

  1. Loop until a digit is found; use for ... else for the “not found” case.

Solution

Python
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.

Advertisement

Learn the concept

New to this topic? Read the lesson, then come back.

More loop programs

← All 90 Loop Programs