'Python, checking if string consists only of 1's and 0's

I'm trying to write a function that checks if given string is binary or not. I'm using a while loop to browse characters in string one by one. If all of them are 0's or 1's it's going to return True, if anything is not - break the loop and return False. I've just written this tiny part of code, but it doesn't run. Why?

def fnIsBin(string):
    count = 0
    while count < len(string):
        character = string[count]
        if character == '0' or character == '1':
            print (count, character[count], "OK")
            count = count+1
            continue
        else:
            print (count, character[count], "ERROR")
            return False
            break

EDIT:
I also tried using 'set' to elude iterating with loop, but i don't quite get how does "set(string)" work. I got error that i cant consider it as a list. So how can I compare elements to 0 & 1?

def fnIsBin(string):
charactersList = set(string)
if len(charactersList) > 2:
    return False
else:
    if (charactersList[0] == '0' or charactersList[0] == '1') and (charactersList[1] == '0' or charactersList[1] == '1'): #there i made error, how to deal with it?
        return True
    else:
        return False


Solution 1:[1]

You're printing OK for each character that's 0 or 1, even though there may be a later character that isn't. You need to wait until the end of the loop to know that it's really OK.

def fnIsBin(string):
    for character in string:
        if character != '0' and character != '1':
            print (character, "ERROR")
            return False
    print "OK"
    return True

There's no need for break after return False -- returning from the function automatically breaks out of the loop.

Solution 2:[2]

Your function fails because you never actually return True. That means if the string is actually binary, you'd return None which Python would consider as False in any boolean check.

Simply add return True at the end of the function.

As @Barmar mentioned in the comment, you also print the value of character[count] instead of string[count] which would cause IndexError.

An easier way to check if the string is binary would be:

test_string = '010101'
all_binary = all(c in '01' for c in test_string)

Solution 3:[3]

There are many ways to do this:

Set:

fnInBin(s):
    return set(s).issubset({'0', '1'}) and bool(s)

Regular expression:

fnInBin(s):
    return bool(re.fullmatch('[01]+', s))

Integer conversion:

fnIsBin(s):
    try:
       int(s, 2)
       return True
    except ValueError:
       return False

The last one will strip whitespace from the input, though.

fnIsBin('\n 1   ') == True

Solution 4:[4]

You can use the int object and give it a base. It will fail if the object passed doesn't consist of a binary representation. Recall that a binary string is base 2.

def fnIsBin(string):
    try:
        binary_repr = int(string, 2)
    except ValueError as err
        print("error: {0}".format(err))
        return False
    return True

Solution 5:[5]

Here's one of the ways you could rewrite your function and maintain a similar structure (looping through each character):

def fnIsBin(string):
    for character in string:
        if not character in '01':
            return False
    return True

You could also do it using regular expressions:

import re
def fnIsBin(string):
    return re.match(r'^[10]+$', '01') is not None

Solution 6:[6]

My two cents (A short way):

test_string = '010101'
result = set(test_string) <= {'0','1'}
print(result)

Using set(test_string) converts the string into list of characters,

{0,1,0,1,0,1}

Using <= operator checks whether the set on the left-side is a proper subset of the set on the right-hand side

Ultimately checking whether there are only 0 and 1 are in the string or not in an in-direct and mathematical way.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Barmar
Solution 2
Solution 3
Solution 4
Solution 5
Solution 6 DJ Hemath