Padding

"I am currently a Software Engineering student at ALX. I'm passionate about technology and enjoy conducting research to find answers on my own. I have a natural inclination to ask 'WHY' more often than 'HOW'.
"While working on projects at ALX, I have acquired a wealth of interesting and diverse knowledge about software engineering and computer science in general. Therefore, I needed a place to store and save all this information, allowing me to refer back to it whenever I forget."
>>> 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)
' 42572654 YES votes 49.67%'
The underscores between the numbers are a way to make long numbers readable in Python, python ignores it when it finds it between number literals.
The :-9 value indicates padding. If you remove the - the effect will be the same. However, according to the docs, the - indicates that a sign should be used only for negative numbers (this is the default behavior).
When the number passed is not up to 9, spaces are padded in front of the number to make it 9 characters
If you do this instead:
>>> yes_votes = 42_572_654
>>> no_votes = 43_132_495
>>> percentage = yes_votes / (yes_votes + no_votes)
>>> '{:09} YES votes {:2.2%}'.format(yes_votes, percentage)
'042572654 YES votes 49.67%'
0 will be used.
Example:Replacing %+f, %-f, and % f and specifying a sign:
>>> '{:+f}; {:+f}'.format(3.14, -3.14) # show the positive sign always
'+3.140000; -3.140000'
>>> '{: f}; {: f}'.format(3.14, -3.14) # show a space for positive numbers
' 3.140000; -3.140000'
>>> '{:-f}; {:-f}'.format(3.14, -3.14) # show only the minus -- same as '{:f}; {:f}'
'3.140000; -3.140000'
The format specifiers %+f, %-f, and % f are used in Python to control the formatting of floating-point numbers. Here's a breakdown of each specifier:
%+f: This specifier is used to display a floating-point number with a sign. It will include a plus sign (+) for positive numbers and a minus sign (-) for negative numbers.Example:
number = 10.5 formatted_number = '%+f' % number print(formatted_number)Output:
+10.500000%-f: This specifier is used to left-align the floating-point number within the specified field width, adding spaces to the right if necessary.Example:
number = 10.5 formatted_number = '%-10f' % number print(formatted_number)Output:
10.500000% f: This specifier is used to include a space character before positive numbers, adding a minus sign for negative numbers.Example:
number1 = 10.5 number2 = -10.5 formatted_number1 = '% f' % number1 formatted_number2 = '% f' % number2 print(formatted_number1) print(formatted_number2)Output:
10.500000 -10.500000
These format specifiers can be useful for controlling the appearance of floating-point numbers in formatted strings.
The '%+f' % number expression is an older style of formatting in Python known as the "printf-style" formatting.
In this expression, % is the formatting operator, and number is a variable representing the floating-point number you want to format. The %+f specifies the format for the number.
>>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
>>> for name, phone in table.items():
... print(f'{name:10} ==> {phone:10d}')
...
Sjoerd ==> 4127
Jack ==> 4098
Dcab ==> 7678
In the given code snippet, a dictionary named table is defined with keys representing names and values representing phone numbers. The for loop iterates over the items in the dictionary using the items() method. The name variable captures the key (name), and the phone variable captures the value (phone number) for each iteration.
Within the loop, the print() function is called to display the name and phone number in a formatted manner using f-strings. Let's break down the formatting:
{name:10}: This specifies the formatting for thenamevariable. The:10part indicates that the field width should be 10 characters. If the name is less than 10 characters, it will be padded with spaces on the right to reach the specified width.==>: This is a literal string that will be displayed as-is.{phone:10d}: This specifies the formatting for thephonevariable. The:10dpart indicates that the field width should be 10 characters, anddspecifies that the value should be formatted as a decimal integer. If the phone number is less than 10 digits, it will be padded with spaces on the left to reach the specified width.
The output of the code snippet will be as follows:
Sjoerd ==> 4127
Jack ==> 4098
Dcab ==> 7678
Each line displays the name, followed by the ==>, and then the phone number. The names are displayed with a field width of 10 characters, while the phone numbers are displayed as decimal integers with a field width of 10 characters.
This is another important feature of using :-9
>>> '12'.zfill(5)
'00012'
>>> '-3.14'.zfill(7)
'-003.14'
>>> '3.14159265359'.zfill(5)
'3.14159265359'



