Given a decimal number, consider how to get a hexadecimal number. As an example, using the int "227", I want the hexadecimal number "e3" below.
If you think about it normally, it's like this
hex(227)
'0xe3'
I don't want 0x
.
You can replace it as a string, but ...
hex(227).replace('0x','')
'e3'
It ’s not very clear.
There is also such a thing
'{:x}'.format(227)
'e3'
Somewhat better than replace. .. ..
But isn't it something that doesn't go smarter?
I told you in the comments. If it is 3.6 or later, it seems that you can write it like this using ** formatted string literal **.
f'{227:02x}'
http://docs.python.jp/3.6/reference/lexical_analysis.html#f-strings
It feels like a more abstract string format with%. Unfortunately, many DCC tools are not yet compatible with Series 3. .. .. Wait 2019 (was it?
int('e3',16)
227
int('0xe3',16)
227
It takes a lot of work to get e3 from 227, but you can get 227 from e3 in one shot, right?
Recommended Posts