python - Python 3 中是否有比 bytes(str(some_variable), 'utf-8' ) 更短或更快的替代方案?

标签 python string python-3.x type-conversion arrays

例如,将 5638463263 这样的整数转换为其“字节字符串化”版本的最佳方法是什么? b'5638463263'

我可以写bytes(str(5638463263), 'utf-8'),但这对我来说似乎有点冗长。有什么简写或标准库函数可以做得更好吗?

最佳答案

str(5638463263).encode()

更短。

在 Python 3.2 及更高版本中,str.encode 使用 'utf-8' 作为默认编码。

关于python - Python 3 中是否有比 bytes(str(some_variable), 'utf-8' ) 更短或更快的替代方案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19699647/

相关文章:

C#:当只有项目名称的一部分已知时,如何找到项目的索引

Java使用数组将英语转换为Piglatin

python-3.x - 在python3中停止线程

python - 如何根据另一列替换 numpy 数组中的值?

python - 如何在 Python plt 中获取最新绘制线的颜色

linux - 如何从所有行中删除最后一个字符,但前提是它是某个字符

python - 类型错误 : unsupported operand type(s) for -: 'str' and 'int'

python - 从 shell 控制台手动运行 python3 脚本和命令

python - Sphinx for Python 中函数或类的交叉引用文档

python - Pandas 数据帧 : Replace based on filter and regex extract