Python Practice › String Programs › Program 95
Is a string binary?
Write a Python program to check whether a string contains only 0 and 1.
Input
"01011100", "0120"
Output
01011100 yes 0120 no
Approach
- The set of characters must be a subset of {“0”, “1”}.
Solution
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.