简体   繁体   English

在 python 中使用 b'' like f 字符串

[英]Using b'' like f string in python

I have this string s = 'ha\xcc\x81nh' .我有这个字符串s = 'ha\xcc\x81nh' I want to use as byte, is there anything as b'{s}' like f string f'{s}'我想用作字节,有什么像b'{s}'这样的 f string f'{s}'

Normally we can use the following code to convert a string to a byte string.通常我们可以使用以下代码将字符串转换为字节字符串。

b = sample_string.encode()

If we use this method in your use-case it would be like this.如果我们在您的用例中使用此方法,它将是这样的。

b = f'{s}'.encode()

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

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