format float number to 17 chars - Python - python

I'm new to Python, and I'm struggling to format a number to 17 chars including the decimal point.
example:
100.00
result:
0000000100.000000
I tried .zfill(17), but it only displays one digit after the decimal point.

Use str.format:
>>> '{:017.6f}'.format(100.00)
'0000000100.000000'
or format:
>>> format(100.00, '017.6f')
'0000000100.000000'

I like this:
In [42]: '%017.6f'%100
Out[42]: '0000000100.000000'

Related

Using commas in dollar values in a printed table

I'm printing a table - several rows containing various variable types -
example:
print('{:10s} ${:12.0f} {:10.1f}%'.format(ID,value,change))
first $324681 2.4%
where the integers 10, 12, and 10 provide the column spacing I want.
But I want to have the $ amounts printed with a comma separator, thus:
print('{:10s} ${:,.0f} {:10.1f}%'.format(ID,value,change))
first $324,681 2.4%
But this loses the '12' spaces allowed for the second item.
But when I try
print('{:10s} ${:,12.0f} {:10.1f}%'.format(ID,value,change))
I get "ValueError: Invalid format specifier"
How can I get both the commas and control over the column spacing?
Python 3.6 running in Spyder.
This should do the trick:
print('{:10s} ${:1,d} {:10.1f}%'.format('first', 324681, 2.4))
OUTPUT:
first $324,681 2.4%
If you are content to have the specified total width but the dollar in a fixed column possibly separated from the digits, then you could just do what you were doing, but with the 12 before the ,.
>>> value = 324681
>>> ID = "first"
>>> change = 2.4
>>> print('{:10s} ${:12,.0f} {:10.1f}%'.format(ID,value,change))
first $ 324,681 2.4%
If you want the numbers to follow immediately after the $, then you can format it as a string without any padding, and then use the string in a fixed-width format specifier:
>>> print('{:10s} {:13s} {:10.1f}%'.format(ID ,'${:,.0f}'.format(value), change))
first $324,681 2.4%
or:
>>> print('{:10s} {:>13s} {:10.1f}%'.format(ID ,'${:,.0f}'.format(value), change))
first $324,681 2.4%
(The width specifier is increased to 13 here, because the $ sign itself is in addition to the 12 characters used for the number.)

What does %xP mean in python?

I came across this line of code and I'm having a tough time figuring out what %xP is doing.
result = "0x%xP"%address
Isn't %address a modulus or is this performing some kind of formatting?
It's a format string. It's a literal 0x followed by the address as a hexidecimal number (%x) followed by a literal P.
Some examples:
>>> '0x%xP' % 1
'0x1P'
>>> '0x%xP' % 10
'0xaP'

Python integer to hex string with padding

Consider an integer 2. I want to convert it into hex string '0x02'. By using python's built-in function hex(), I can get '0x2' which is not suitable for my code. Can anyone show me how to get what I want in a convenient way? Thank you.
integer = 2
hex_string = '0x{:02x}'.format(integer)
See pep 3101, especially Standard Format Specifiers for more info.
For integers that might be very large:
integer = 2
hex = integer.to_bytes(((integer.bit_length() + 7) // 8),"big").hex()
The "big" refers to "big endian"... resulting in a string that is aligned visually as a human would expect.
You can then stick "0x" on the front if you want.
hex = "0x" + hex
>>> integer = 2
>>> hex_string = format(integer, '#04x') # add 2 to field width for 0x
>>> hex_string
'0x02'
See Format Specification Mini-Language

Python: converting single digit decimal to hex

Is there any way to get hex (5) output '0x05' instead of '0x5'?
For example i want to convert [255,11,132] to hex string like 'ff0b84' So i can slice it by 2 characters and covert to decimal again. But python doesn't put 0 before b so i can't slice string by 2 characters!
I think you could use format to get '0x05' instead of '0x5':
In [64]: format(5, '#04x')
Out[64]: '0x05'
In [65]: format(15, '#04x')
Out[65]: '0x0f'

Python Hexadecimal

How to convert decimal to hex in the following format (at least two digits, zero-padded, without an 0x prefix)?
Input: 255 Output:ff
Input: 2 Output: 02
I tried hex(int)[2:] but it seems that it displays the first example but not the second one.
Use the format() function with a '02x' format.
>>> format(255, '02x')
'ff'
>>> format(2, '02x')
'02'
The 02 part tells format() to use at least 2 digits and to use zeros to pad it to length, x means lower-case hexadecimal.
The Format Specification Mini Language also gives you X for uppercase hex output, and you can prefix the field width with # to include a 0x or 0X prefix (depending on wether you used x or X as the formatter). Just take into account that you need to adjust the field width to allow for those extra 2 characters:
>>> format(255, '02X')
'FF'
>>> format(255, '#04x')
'0xff'
>>> format(255, '#04X')
'0XFF'
I think this is what you want:
>>> def twoDigitHex( number ):
... return '%02x' % number
...
>>> twoDigitHex( 2 )
'02'
>>> twoDigitHex( 255 )
'ff'
Another solution is:
>>> "".join(list(hex(255))[2:])
'ff'
Probably an archaic answer, but functional.

Categories

Resources