我目前正在将一些C++代码移植到python,但没有找到如何翻译这个代码:
print std::setfill('0') << std::setw(2) << std::hex << myVar << std::dec << " "如何将std::setfill('0')和std::setw(2)翻译成python?
发布于 2014-06-11 14:59:15
没有直接的等效值,但是可以用format函数转换要显示的每个值。有关格式规范,请参见https://docs.python.org/2/library/string.html#format-specification-mini-language。
print '{:02x}'.format(myVar)发布于 2017-03-03 13:26:06
我来这里是想找一个和std::setw(n)相当的东西。出于好奇,我使用了'{:>n}'.format(nbr)。
示例:
In [13]: '{:>2}'.format(1)
Out[13]: ' 1'
In [14]: '{:>2}'.format(10)
Out[14]: '10'https://stackoverflow.com/questions/24165993
复制相似问题