1

I tried to generate random string with

python3 -c 'import base64, os; print(base64.b64encode(os.urandom(24)))'

The output is:

b'32randomstring'

Is it possible to remove the b'' ?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Jason Rich Darmawan
  • 1,607
  • 3
  • 14
  • 31
  • 3
    Does this answer your question? [Suppress/ print without b' prefix for bytes in Python 3](https://stackoverflow.com/questions/16748083/suppress-print-without-b-prefix-for-bytes-in-python-3) – Lucan May 26 '20 at 10:20
  • Thank you for pointing it out. I appreciate it but I found it too complicated and went with Felk's ansswer `python3 -c "import base64, os; print(base64.b64encode(os.urandom(24)).decode('ascii'))"` – Jason Rich Darmawan May 26 '20 at 12:33

1 Answers1

1

The b-prefix indicates that you are dealing with a byte string, which is basically just a sequence of bytes. to turn those into text, you need to apply some encoding.

Given that you used base64, all the produced bytes nicely map onto ascii anyway, and you can do something like this:

print(base64.b64encode(os.urandom(24)).decode("ascii"))
Felk
  • 7,720
  • 2
  • 35
  • 65