简体   繁体   中英

How to print without the b`` string?

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''?

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM