python - setfill 和 setw python 等效?

标签 python c++ std

我目前正在将一些 C++ 代码移植到 python,但没有找到如何翻译它:

print  std::setfill('0') << std::setw(2) << std::hex << myVar << std::dec << " "

如何将 std::setfill('0')std::setw(2) 翻译成 python?

最佳答案

没有直接的等效项,但您可以使用 format 函数转换要显示的每个值。参见 https://docs.python.org/2/library/string.html#format-specification-mini-language格式规范。

print '{:02x}'.format(myVar)

关于python - setfill 和 setw python 等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24165993/

相关文章:

c++ - 错误: ‘invoke’ is not a member of ‘std’

python - 编译时如何捕获更改的 .c 文件列表?

c++ - 使用 CMake 创建 Qt 翻译文件

c++ - 如何停止 IntelliSense PCH 警告?

c++ - 将模拟对象分配给 protected 成员

c++ - 为什么 complex<double> 上的 operator>> 在到达 EOF 时不设置 eofbit?

python - 如何迭代类方法

python - 使用 groupby 和 last 拆分 Pandas Dataframe

python - 使用 scipy 的 SmoothSphereBivariateSpline 进行插值

c++ - 将 true/false 分配给 std::string:这是怎么回事?