Input and output question

I was reading python document and came across this code. I wonder what

{:-9}

{:2.2%}

does on the fourth line?

yes_votes = 42_572_654
no_votes = 43_132_495
percentage = yes_votes / (yes_votes + no_votes)
'{:-9} YES votes  {:2.2%}'.format(yes_votes, percentage)

You must select a tag to post in this category. Please find the tag relating to the section of the course you are on E.g. loops, learn-compatibility

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

putting this into python, this is what I see -

The _ characters in yes_votes and no_votes end up not doing anything. My guess is that because the first characters are integers the whole value in interpreted as an integer. The _'s are essentially thrown out. This is supported by the fact that type(yes_votes) returns int

The {:-9} seems to force the string to print the value in yes_votes with at least 9 characters. {:-1} and {:-5} both print the entire number. The effect is that you left-pad the output until you hit at least x characters in {:-x}. This is supported by {:-90} left-padding the output with a ton of empty space characters. Why the author put a - in there I have no idea and I dont feel like testing more to figure it out.

the {:-2.2%} is a bit more interesting. Again I am unsure why the number is negative. The integer portion on -2.2 serves the same purpose as the -9. It will ensure that at least 2 characters are printed, even if one is a space. The % tell python that the value is a 0-1 percentage, but to print as a 0-100 percentage eg .5 should be printed as 50%. The .2 looks like it tells python how many significant digits to use after the decimal, but only when we have % present at the end. If there is no % then it will tell python how many overall significant digits to use.

2 Likes

Those underscores are just used for the variable name/identifier, they’re not involved in the formatting of this example-

this_is_a_string = "Hello"  # a long name but still just a name for this string.

Fortunately you don’t need to guess, string formatting is covered in detail in a number of locations. As per the docs ‘-’ indicates that a sign should be used only for negative numbers (this is the default behavior). so no + symbol for positive numbers.

A fairly nice intro to string formatting in Python-
https://pyformat.info/

Python documentation on string formatting
https://docs.python.org/3/library/string.html#formatstrings

2 Likes

Just for clarity, I was referring to the _'s in 42_572_654 and 43_132_495, not the ones in the variable names. Sorry that that was confusing.

1 Like

Those are simply used to help the programmer keep better track of the digits. Useful when dealing with large figures. They will not output as such though! As you suspect, they do nothing (other than make life easier for us).

print(10_000)
# 10000

Ah, my apologies. @toastedpitabread covered that one nicely anyway.

1 Like