Python program to check if a string holds binary content:
In this post, we will learn how to check if a string holds binary content or not in python. For example string 10110101 is a binary string but hello world is not. Binary string holds only 1 and 0. If a string holds any other character, then it is not a binary string.
We will write one python program that will accept one string as input from the user and print one message that it is binary or not.
We can solve this problem in many ways. We can either loop through the characters of the string one by one to verify each character. Another way is to use a set. I will show you both of these methods one by one.
Method 1: By iterating through the characters:
This is the basic way to solve this problem. Our program will iterate through the characters of the string one by one and if it find any character which is not 1 or 0, it will print that the string is not a binary string.
We will create one new method to solve it.
Below is the complete python program:
def check_binary(s):
binary_str = '10'
for ch in s:
if ch not in binary_str:
return False
return True
given_string = input('Enter a string : ')
if check_binary(given_string):
print('Entered string is a binary string')
else:
print('Entered string is not a binary string')
Here,
- check_binary is the method to check if a given string is binary or not. It takes one string as the parameter and returns one boolean value.
- binary_str is a string ‘10’.
- The for loop runs through the characters of the string one by one. If any character is not in binary_str, i.e. if the character is neither 0, nor 1, it returns False.
- Else, once the for loop ends, it returns True i.e. the provided string is a binary string.
If you run the above program, it will print one output as like below:
Enter a string : hello
Entered string is not a binary string
Enter a string : 101
Entered string is a binary string
Enter a string : 1
Entered string is a binary string
Enter a string : hello110
Entered string is not a binary string
Method 2: By using set:
We can also use set to find out if a string is binary or not. We can pass one string to the set constructor set(). It will remove all duplicate characters from the string. If the string is a binary string, it will hold either only 0 and 1, or 0 or 1.
So, we can use a simple if-else block to check that.
Below is the complete program:
def check_binary(s):
binary_set = {'0', '1'}
given_set = set(s)
if given_set == binary_set or given_set == {'0'} or given_set == {'1'}:
return True
else:
return False
given_string = input('Enter a string : ')
if check_binary(given_string):
print('Entered string is a binary string')
else:
print('Entered string is not a binary string')
We are only changing the check_binary method in this program. If you run the above program, it will print outputs similar to the above program:
Enter a string : hello
Entered string is not a binary string
Enter a string : 1011
Entered string is a binary string
Enter a string : 1111
Entered string is a binary string
Enter a string : 0000
Entered string is a binary string