5

I want to encode some data as Base64, and then have the encoded data concatenated in a string.

when I do:

four=base64.urlsafe_b64encode(bytes(MAIL, "utf-8"))
print (four)

result will be:

b'YWxleEBhbGV4LmFsZXg='

I want to remove the b'' from four. So that only YWxleEBhbGV4LmFsZXg= is shown. How do I go about getting just the string YWxleEBhbGV4LmFsZXg= withouth the byte type?

0

1 Answer 1

10

You have a bytes object; decode it to Unicode:

print(four.decode('ascii'))

Base64 only uses ASCII characters, so that's a good codec do use here. If you don't explicitly decode, print() can only use the repr() representation, which produces Python literal syntax, the syntax you'd use to create the same value as a literal.

Sign up to request clarification or add additional context in comments.

1 Comment

worked like a charm. Thank you for the explanation. I used hexdigest() for a sha1 hash that also had extra text, but this wouldn't work for the base64 encoded element. the .decode('ascii') worked though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.