Python Program to Check if a given string is a binary string or not
In this tutorial, you will learn to check if a given string is a binary string or not in Python. Strings in Python are a sequence of characters wrapped in single, double, or triple quotes. Binary strings are the strings that have characters either 1 or 0. If there is any character that is not 1 or 0, then the string is a non-binary string.
Let us look at the sample input and output of the program.
Input: "010010"
Output: binary
Input: "001120001"
Output: non-binary
To solve this problem in Python we can follow these approaches:
- set approach
- iterative approach
Approach 1: set approach
We will be using a set of 0's and 1's to check if the string is binary. Convert the string to a set then we will check if the string is the same as the set of 0's and 1's. If yes, then we will print that the string is binary. Else, we will print that the string is non-binary.
To convert string to set we will use the set() method. It converts any of the iterable or data to a set.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define a function that will accept the string and check if the string is binary or not
Step 2- Declare a set with values 0 and 1
Step 3- Check if the string set is the same as the set of 0 and 1
Step 4- If the condition is satisfied, print that the string is binary
Step 5- Else print that the string is non-binary
Python Program 1
Look at the program to understand the implementation of the above-mentioned approach. We have used the if conditional statement to check if the string set is the same as the set of 0 and 1.
def check(string) :
b = set(string)
s = {'0', '1'}
if s == b or b == {'0'} or b == {'1'}:
print("Binary String")
else :
print("Non Binary String")
s1= "00110101"
# function calling
check(s1)
s2 = "1010100200111"
check(s2)
Binary String
Non Binary String
Approach 2: iterative method
In this approach, we will first declare a string with a value as 0, 1. Then we will compare the characters of this string with the characters of the given string using a loop in Python. We will check if the character of the given string is the character in the string with value 0 or 1.
Algorithm
Follow the algorithm to understand the approach better
Step 1- Define a function that will accept the string and check if the string is binary or not
Step 2- Declare another string with value 01
Step 3- Declare a flag variable
Step 4- Check for every character in the string, if the character is in the 01 string or not
Step 5- If False, update the value of the flag and break out of the loop
Step 6- At the end of the loop, check if the flag is updated or not
Step 7- If the flag is not updated, print binary
Step 8- Else, print non-binary
Python Program 2
Look at the program to understand the implementation of the above-mentioned approach. We have used a flag variable which will help us to check if there is any character except 0 or 1 in the string.
def check(s):
binary="01"
flag=0
for char in s:
if char not in binary:
flag=1
break
else:
pass
if flag==1:
print("Not binary")
else:
print("Binary")
s1="101001"
#function calling
check(s1)
s2="02301"
check(s2)
Binary
Not binary
Conclusion
In this tutorial, we have seen two approaches for checking if a given string is binary or not using the set approach and the iterative approach.