2

Given the python code

import base64
byte = base64.b64encode(b'hello')

byte has the value

b'aGVsbG8='

But I'm trying to get the string of the literal encoded bytes inside the b'...' notation.

Only solution I've found so far is to cast the bytes as string and get the substring of the value without the first two and last characters,

str(byte)[2:-1]

which gives me

'aGVsbG8='

While this works, it feels like a complete hack.

2
  • byte.decode("utf-8") Commented Nov 3, 2018 at 17:04
  • 1
    Perhaps it's worth tagging this question with python3. Commented Nov 3, 2018 at 17:05

2 Answers 2

3

Try this one also works:

str(byte, 'utf-8')
Sign up to request clarification or add additional context in comments.

Comments

2

Decode it as ASCII:

byte.decode('ascii')

1 Comment

Just using decode() would probably work as well...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.