Python Practice › String Programs › Program 95

Is a string binary?

EasyString Programs · 95 of 106

Write a Python program to check whether a string contains only 0 and 1.

Advertisement
Input
"01011100", "0120"
Output
01011100 yes
0120 no

Approach

  1. The set of characters must be a subset of {“0”, “1”}.

Solution

Python
for s in ["01011100", "0120"]:    print(s, "yes" if s and set(s) <= {"0", "1"} else "no")
Output
01011100 yes
0120 no

Learn the concept

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

More string programs

← All 106 String Programs

Leave a Comment