简体   繁体   中英

Unknown format code 'b' for object of type 'str'

def reverseBits(self, n):
    binary = '{0:08b}'.format(n)
    rev = reversed(binary)
    print(rev)
    return rev

I get the error:

ValueError: Unknown format code 'b' for object of type 'str'

It says that this error occurs on line 20 even though there is no line 20.

What am I doing wrong here?

I believe you are trying to convert a string to binary. First it must be converted to Int. What I mean is, say you are trying to get the reverse bits of the number 16 . In Base 2, 16 is 10000 and Reverse that is 00001 . The parameter you pass to the .format Must be of type int. Dont do (...).format("16") instead do (...).format(16)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM