In this program, we will take a string as input and check if a given string is binary or not.
Steps to solve the program
- Take a string as input.
- Create a variable count and assign its value equal to 0.
- Use for loop to iterate over each character in the string.
- If the character is 0 or 1 then add 1 to the count variable.
- If the value of the count variable is equal to the length of the string then string is binary.
- Print the output.
#Input string
str1 = "01011100"
count = 0
for char in str1:
if char == "0" or char == "1":
count += 1
#Printing output
if count == len(str1):
print("Yes")
else:
print("No")
str1 = "sqatools 100"
count = 0
for char in str1:
if char == "0" or char == "1":
count += 1
if count == len(str1):
print("Yes")
else:
print("No")
Output :
Yes
No