setfill 和 setw python 等效吗?

NEL*_*UNI 8 c++ python std

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

print  std::setfill('0') << std::setw(2) << std::hex << myVar << std::dec << " "
Run Code Online (Sandbox Code Playgroud)

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

Mar*_*som 5

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

print '{:02x}'.format(myVar)
Run Code Online (Sandbox Code Playgroud)