Muhammad Axif
Muhammad Axif

Reputation: 1

AttributeError: 'str' object has no attribute 'decode' Having this error on the second line of my code

This is my code,

if __name__ == "__main__":
    key = "0123456789abcdef0123456789abcdef".decode('hex')  /this line is having error
    plain_1 = "1weqweqd"
    plain_2 = "23444444"
    plain_3 = "dddd2225"
    print(plain_1)
    print(plain_2)
    print(plain_3)

    cipher = Present(key)

Output

AttributeError: 'str' object has no attribute 'decode'

Upvotes: 0

Views: 13888

Answers (1)

mikael briolet
mikael briolet

Reputation: 31

It's because you try to decode a string. bytes type can be decoded but not str type. You should encode (key.encode()) this (or use b"foo") before, to convert the string to a bytes object.

>>> foo = "adarcfdzer"
>>> type(foo)
<class 'str'>
>>> foo = foo.encode()
>>> type(foo)
<class 'bytes'>
>>> foo = foo.decode()
>>> type(foo)
<class 'str'>

Upvotes: 2

Related Questions