Python Forum

Full Version: fmt_directory_entry = "{counter:>02}. {director:<52} {avg}"
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
What exactly is going on with the {counter:>02} parameter in the fmt_directory_entry assignment? In particular, how is it effected that 0 should be inserted to the left of counter if len(counter) > 2? Is there a way I could make it be an other symbol other than 0, say the letter i?

def print_results():
    fmt_directory_entry = "{counter:>02}. {director:<52} {avg}"
    for counter, director_info in enumerate(top_directors_with_at_least_four(),
                                            1):
        director, rating = director_info[0]

        print(fmt_directory_entry.format(counter=counter, director=director,
                                         avg=rating))
Output:
01. Sergio Leone 8.5 02. Christopher Nolan 8.4 03. Quentin Tarantino 8.2 ... 10. Alfonso Cuarón 7.8 11. Peter Jackson 7.7 12. Martin Scorsese 7.7
Hi buran,

I've read through the documentation, but I can't seem to find what I'm looking for - especially with regards to why that 0 does what is does. Do you think you could give me a little bit further guidance?
{counter:>02}
counter is the name that you want formated
> means it should be right-aligned
0 - fill-in symbol if len is less than the required length
2 - the required length

in the link I shared, in the first green box, the first row format_spec explains it
Quote:format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.precision][type]
fill ::= <any character>
align ::= "<" | ">" | "=" | "^"
sign ::= "+" | "-" | " "
width ::= digit+
grouping_option ::= "_" | ","
precision ::= digit+
type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
Hmm... So I take that we wouldn't be able to replace the 0 stand-in with some other symbol?

Quote:The '#' option causes the “alternate form” to be used for the conversion. The alternate form is defined differently for different types. This option is only valid for integer, float, complex and Decimal types. For integers, when binary, octal, or hexadecimal output is used, this option adds the prefix respective '0b', '0o', or '0x' to the output value. For floats, complex and Decimal the alternate form causes the result of the conversion to always contain a decimal-point character, even if no digits follow it. Normally, a decimal-point character appears in the result of these conversions only if a digit follows it. In addition, for 'g' and 'G' conversions, trailing zeros are not removed from the result.
from https://docs.python.org/2/library/string.html
Aligning the text and specifying a width:
>>> '{:<30}'.format('left aligned')
'left aligned                  '
>>> '{:>30}'.format('right aligned')
'                 right aligned'
>>> '{:^30}'.format('centered')
'           centered           '
>>> '{:*^30}'.format('centered')  # use '*' as a fill char
'***********centered***********'
Look at the last example and the useful use comment
Sorry, my mistake, your format string is incorrect, because you uses the 0 that is the prefix
the correct sequence is fill, allign

# replace with space
for counter in range(9,11):
    print(f'{counter: >2}.')

print('\n-----------------\n')

# replace with #
for counter in range(9,11):
    print(f'{counter:#>2}.')
Output:
9. 10. ----------------- #9. 10.
You can also use Unicode as fill sign.
print('{:\N{FACE WITH OPEN MOUTH VOMITING}^30}'.format('Corona'))
Alternative
"Corona".center(30, "\N{BEER MUG}")
https://docs.python.org/3/library/stdtyp...ng-methods

Another good documentation is https://pyformat.info/.
The f-strings are not explained there, but this is the same syntax as for format.