Im trying to figure out how to check if the input consists of only "0" and "1". Been stuck on this for abit now, im quite new to python. I got the inverse part down, just need help with the check. Thanks!
Asked
Active
Viewed 40 times
1 Answers
-1
You can cast it to set()
and check if it only contains "0" and "1"
value = "001001"
binary = set(value) <= {"0", "1"}
print(binary)
Result:
True
Trying with different values from "0" and "1":
value = "hello"
binary = set(value) <= {"0", "1"}
print(binary)
Result:
False

Leonardo Lima
- 373
- 2
- 10