Python Practice › String Programs › Program 31

Does a string contain a number?

EasyString Programs · 31 of 106

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

Advertisement
Input
"python1", "python"
Output
python1 has a number
python has no number

Approach

  1. any(ch.isdigit() for ch in text).

Solution

Python
for text in ["python1", "python"]:    has = any(ch.isdigit() for ch in text)    print(text, "has a number" if has else "has no number")
Output
python1 has a number
python has no number

Line by line

Line 2

any() stops at the first True.

Advertisement

Learn the concept

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

More string programs

← All 106 String Programs