2

Consider the following code...

base64EncodedCredentials = base64.b64encode(b"johndoe:mysecret")
print(base64EncodedCredentials)

the response I get back is

b'am9obmRvZTpteXNlY3JldA=='

Why does it have a b before the string? How can I get just the string value of 'am9obmRvZTpteXNlY3JldA==' instead?

Technically speaking - this question can be considered a duplicate of another question IF you know that the problem is about byte string vs. string. In my case, I asked the question because I did not know that there was something called byte string. For new Python programmers, this question may be beneficial because it uses language they see on their program or debugger. If they don't know what a byte string is, perhaps this question can be useful and provide the translation from their problem to the technical terms used by more fluent Python programmers. The question differs in use of vocabulary.

barrypicker
  • 9,740
  • 11
  • 65
  • 79
  • see https://stackoverflow.com/questions/6224052/what-is-the-difference-between-a-string-and-a-byte-string – liamhawkins Feb 20 '19 at 20:52
  • Because base64 encoded data could be binary, so it returns bytes instead of a string. If you want a string, you need to decode the binary data with the right encoding. Such as `data.decode('utf-8')`, where `data` are the bytes returned from your base64 decoding. – John Szakmeister Feb 20 '19 at 20:54
  • @JohnSzakmeister - thanks - new to Python (probably obvious). so the b'whatever' is printing via print command as a psuedo string, but internally its a byte array? – barrypicker Feb 20 '19 at 21:02
  • That's pretty close to the truth. It's actually a [bytes() object](https://docs.python.org/3/library/stdtypes.html#bytes). – John Szakmeister Feb 20 '19 at 21:13

2 Answers2

3

Refer to this post to get rid of the encoding.

print(base64EncodedCredentials.decode("utf-8"))

jfleach
  • 501
  • 1
  • 8
  • 21
2

It is returning a bytes object. Usually base64 is used to make something 7-bit safe, and thus is often used with byte-oriented (rather than character oriented) data, for example, to shove out a socket.

You can decode it to a string, just like any other bytes object:

output.decode('ascii')

Byte and String objects are changed between each other using encode() and decode(). It is safe to use the ascii codec since base64 is guaranteed to only return 7-bit ascii.

Max
  • 10,701
  • 2
  • 24
  • 48
  • That makes sense. Didn't even know what to call that type - "binary string". Now knowing what it is called, I can do further research. Thanks for the help! – barrypicker Feb 20 '19 at 20:56
  • Yes, `bytes` is a fundamental type in Python. – Max Feb 20 '19 at 20:57
  • Understood. Never saw a base64 encoder that returned anything but a string before. I thought that was the point - it returned a ASCII safe string version of binary object (including string) at the cost of additional space... – barrypicker Feb 20 '19 at 20:59