【问题标题】:Using b'' like f string in python在 python 中使用 b'' like f 字符串
【发布时间】:2021-02-23 03:41:03
【问题描述】:

我有这个字符串s = 'ha\xcc\x81nh'。我想用作字节,有没有像b'{s}'这样的f字符串f'{s}'

【问题讨论】:

标签: python byte encode


【解决方案1】:

通常我们可以使用以下代码将字符串转换为字节字符串。

b = sample_string.encode()

如果我们在您的用例中使用此方法,它将是这样的。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 2014-11-26
    相关资源
    最近更新 更多