Home »
Python »
Python Programs
Python program to check whether a given string is binary or not
Here, we will take a string from the user and then using a Python program, we will check whether a given string is binary or not.
By Shivang Yadav Last updated : March 04, 2024
Python programming language is a high-level and object-oriented programming language. Python is an easy to learn, powerful high-level programming language. It has a simple but effective approach to object-oriented programming.
Strings in Python are immutable means they cannot be changed once defined.
Binary string
Binary string is a special type of string in which we have only 0 and 1 as characters of the string.
Example: "01101001"
Check whether a given string is binary or not
We will take a string as input from the user. And then check whether a given string is binary or not and return true or false based on this.
Example
Consider the below example with sample input and output:
Input: "011000101"
Output: Binary String
To check if the given string is a binary string or not. We will loop over the string and if any element is other than '1' or '0' then it is not a binary string otherwise it is a binary string.
Algorithm
- Loop over the string, i = 0 to str.length
- if str[i] != '0' or str[i] != '1' -> not binary, break
- Exit
Python program to check the whether a given string is binary or not
# Python program to check whether a string
# is a binary string or not
# Getting string input from the user
myStr = input('Enter the binary string : ')
# check whether a string is binary string or not
flag = True
for char in myStr :
if(char == '0' or char == '1'):
continue
else :
flag = False
print("The String is not a binary string")
break
if(flag):
print("The String is binary string")
Output
Enter the binary string : 01001110
The String is binary string
Another Approach
Another approach to solve the problem is by using sets to store the characters of the binary string. We will store '0' and '1' to the set and then compare it with the characters of the string, if there exists any character other than those present in the set return false otherwise return true.
Program to check whether a given string is binary or not
# Python program to check whether a string
# is a binary string or not
# Getting string input from the user
myStr = input('Enter the binary string : ')
# check whether a string is binary string or not
strSet = set(myStr)
binValues = {'0', '1'}
if binValues == strSet or strSet == {'0'} or strSet == {'1'}:
print("The String is binary string")
else :
print("The String is not a binary string")
Output
Enter the binary string : 011010101111
The String is binary string
Explanation
In the above code, we have taken the string as input from the user. And then created two sets one strSet consisting of elements of the string and other binValues consisting of binary values i.e. 0 and 1. Then we have compared values and return the statements based on the comparison.
To understand the above program, you should have the basic knowledge of the following Python topics:
Python String Programs »