简体   繁体   English

如何在没有 b`` 字符串的情况下打印?

[英]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: output 是:

b'32randomstring'

Is it possible to remove the b''?是否可以删除b''?

The b -prefix indicates that you are dealing with a byte string, which is basically just a sequence of bytes. b -前缀表示您正在处理一个字节字符串,它基本上只是一个字节序列。 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:鉴于您使用了 base64,所有生成的字节都很好地 map 到 ascii 上,您可以执行以下操作:

print(base64.b64encode(os.urandom(24)).decode("ascii"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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