How to tell if a string has exactly 8 1's and 0's in it in python -
i want return boolean value true
or false
depending on if string contains 1's , 0's.
the string has composed of 8 1's or 0's , nothing else.
if contains 1's or 0's, return true
, if not return false
.
def isitbinary(astring): if astring == 1 or 0: return true else: return false
this have far i'm not sure how compare both of numbers see if has length of 8 1's , 0's.
you can use all , check length make sure 8.
all(c in '10' c in astring) , len(astring) == 8
example:
astring = '11110000' all(c in '10' c in astring) , len(astring) == 8 >>> true
the main benefit of doing on other methods short-circuit if finds 0 or one.
Comments
Post a Comment