ADVERTISEMENT
ADVERTISEMENT

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.
Submitted by Shivang Yadav, on April 24, 2021

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 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:

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

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.

Python String Programs »



ADVERTISEMENT
ADVERTISEMENT


Comments and Discussions!



ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.