import string input = """ 10101 A 1111111111111 """ input = string.split(input) first = input[0] operator = input[1] second = input[2] width = 1 while len(first) < len(second): first = string.zfill(first, width) width += 1 while len(first) > len(second): second = string.zfill(second, width) width += 1 print first print second, operator length = len(first) i, output = 0, '' while i < length: if operator == 'A': if first[i] == '1' and second[i] == '1': output = output + '1' else: output = output + '0' elif operator == 'O': if first[i] == '1' or second[i] == '1': output = output + '1' else: output = output + '0' elif operator == 'X': if (first[i] == '1' and second[i] == '0') or (first[i] == '0' and second[i] == '1'): output = output + '1' else: output = output + '0' i += 1 if int(output) == 0: output = '0' print print output