I am currently porting some C++ code to Python but didn't find out how to translate this:
std::cout << std::setfill('0') << std::setw(2) << std::hex << myVar << std::dec << " " How do I translate std::setfill('0') and std::setw(2) to Python?
There's no direct equivalent, but you can convert each value you want to display with the format function. See https://docs.python.org/2/library/string.html#format-specification-mini-language for the format specification.
print '{:02x}'.format(myVar) In order to get the same result you can use this:
res = '{0>2}'.format(x) The output using the previous code and printing the result being x = 2 will be:
02 You can also use this format in strings not just for print.
'<any_char>'*nreturns any_char n times."%x"%myVarformats myVar as a hex.myVarhas a semantic such that it requires such formatting, you'd write a single manipulator which does that, to be used with all variables which have that semantics. (In Python, the only way I can think of off hand to to that is to write a function which does the conversion, using the desired formatting, and returns a string.)std::dec; you'd reset it to whatever it was before you changed it. (This can be done in the destructor of a custom manipulator, or by use of a scoped object which saves the format in its constructor, and restores it in its destructor.)