Each time through the loop, multiply the current value by 2 and add the next digit's integer value:
B = '1101' # Convert binary digits to integer with ord I = 0 # from ww w .j a va 2 s . co m while B != '': I = I * 2 + (ord(B) - ord('0')) B = B[1:] print( I )
A left-shift operation (I << 1) would have the same effect as multiplying by 2 here.
You can use the following function to convert int to binary back and forth.
print( int('1101', 2) ) # Convert binary to integer: built-in print( bin(13) ) # Convert integer to binary: built-in