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

Question:

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
Asked By: bansheemon

||

Answers:

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
Answered By: Ryan

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)
Answered By: kichik

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
Answered By: naktinis

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.

Answered By: Barmar

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
Answered By: Håken Lid

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.

Answered By: user2615011

Adding a XOR solution to the mix:

bin_string = '010101'
for j in range(0, len(bin_string)):
    if int(bin_string[j]) != 0 ^ int(bin_string[j]) != 1:
        break
Answered By: greystar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.