Given a string str. The task is to check whether it is a binary string or not.
Examples:
Input: str = "01010101010" Output: Yes Input: str = "geeks101" Output: No
Approach 1: Using Set
- Insert the given string in a set
- Check if the set characters consist of 1 and/or 0 only.
# Python program to check # if a string is binary or not # function for checking the # string is accepted or not def check(string) : # set function convert string # into set of characters . p = set(string) # declare set of '0', '1' . s = {'0', '1'} # check set p is same as set s # or set p contains only '0' # or set p contains only '1' # or not, if any one condition # is true then string is accepted # otherwise not . if s == p or p == {'0'} or p == {'1'}: print("Yes") else : print("No") # driver code if __name__ == "__main__" : string = "101010000111" # function calling check(string) |
chevron_right
filter_none
Output:
Yes
Approach 2: Simple Iteration
- Iterate for every character and check if the character is 0 or 1.
- If it is not then set a counter and break.
- After iteration check whether the counter is set or not.
# Python program to check # if a string is binary or not # function for checking the # string is accepted or not def check2(string) : # initialize the variable t # with '01' string t = '01' # initialize the variable count # with 0 value count = 0 # looping through each character # of the string . for char in string : # check the character is present in # string t or not. # if this condition is true # assign 1 to the count variable # and break out of the for loop # otherwise pass if char not in t : count = 1 break else : pass # after coming out of the loop # check value of count is non-zero or not # if the value is non-zero the en condition is true # and string is not accepted # otherwise string is accepted if count : print("No") else : print("Yes") # driver code if __name__ == "__main__" : string = "001021010001010" # function calling check2(string) |
chevron_right
filter_none
Output:
No
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
Recommended Posts:
- Check if given Binary string follows then given condition or not
- Check whether the binary equivalent of a number ends with given string or not
- Check if binary representations of 0 to N are present as substrings in given binary string
- Check if a binary string has a 0 between 1s or not | Set 2 (Regular Expression Approach)
- Check if a binary string contains consecutive same or not
- Check if all the 1's in a binary string are equidistant or not
- Check if a binary string has a 0 between 1s or not | Set 1 (General approach)
- Check if decimal representation of Binary String is divisible by 9 or not
- Python Set | Check whether a given string is Heterogram or not
- Python program to check if a given string is Keyword or not
- Python | Check if given string is numeric or not
- Periodic Binary String With Minimum Period and a Given Binary String as Subsequence.
- Decimal representation of given binary string is divisible by 5 or not
- Program to check if a matrix is Binary matrix or not
- Check if a given string is Even-Odd Palindrome or not
- Check if the given string is linear or not
- Python | Check if a given object is list or not
- Python - Check whether the given List forms Contiguous Distinct Sub-Array or Not
- Python Membership and Identity Operators | in, not in, is, is not
- Python program to check if a string is palindrome or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : nidhi_biet

