# Specifying the number of display digits and filling in zeros

2021年6月5日

Hello everyone. This page is an English translation of a Japanese page. (The original Japanese has been slightly rewritten to make it easier to translate into English.)

Recently, I have been learning Python. In this article, we learned about format specifiers and how to output values. We tried to use format specifiers to display digits and fill in zeros. This article is a reminder of what we learned.

Note that only some of the format specifiers are introduced in this article.

The following is a brief description of the environment in which Python runs.

• Windows 10 64bit
• python 3.9.4
• PyCharm Community Edition 2021.1 x64

More details can be found in previous article.

Sponsored Links

## Formatted string literal

There is a description method called formatted string literal. By writing a string after the letter f or F, you can embed an expression in the string. The expression is enclosed in curly brackets.

In the following sample program, the value of the variable x is embedded in a string and output.

Source
``````x = 'abcd1234′
print(f’x: {x}’)``````
Execution results
``x: abcd1234``

The above program just outputs the value of the variable x.

To specify the format, put a colon ':’ after the variable. After that, specify the format according to the following syntax.

syntax
[[fill]align][sign][#][width][grouping_option][.precision][type]

This time, I tried the above rules related to specifying the number of digits to be displayed and zero-filling.

## Output value in 10 digits

By specifying [width], you can adjust the width of the display digits.

width
Minimum number of digits to be displayed.

This time, we tried to display the value of a variable in 10 digits. The following is the sample program we created.

Source
``````a = 'ABC1234’
b = 123
c = 12.345
d = '12345678901234’
e = 12345678901234

print(f’a:{a:10}’)
print(f’b:{b:10}’)
print(f’c:{c:10}’)
print(f’d:{d:10}’)
print(f’e:{e:10}’)``````
Execution results
``````a:ABC1234
b:       123
c:    12.345
d:12345678901234
e:12345678901234``````

Since the values of variables a, b, and c are smaller than 10 digits, the rest of the display digits are shown as half-width spaces. The characters to fill in the rest can be specified with [fill]. In the above program, that part is omitted, so the default value of one-byte spaces is output.

The character alignment is described in [align], but since it is omitted, it seems that the initial value is set according to the type of the variable. Looking at the above result, it seems to work as follows.

Variable Types Initial value of align
string left justification
Integer, float right justification

Since the values of variables d and e are over 10 digits, they seem to be packed to the left.

## Align Left, Align Right, Align Center

The [align] option allows you to specify how the text will be aligned.

align
Describe the following symbols according to their use:

Symbol Meaning
< Output with left justification.
> Output with right justification.
= If the sign is specified as output, it will be filled in after the sign.
^ The output will be centered.

In the following, we have tried to display strings, positive numbers, and negative numbers.

Source
``````a = 'ABC1234′

print(f’a:{a:<10}’)
print(f’a:{a:>10}’)
print(f’a:{a:^10}’)

print('—')

b = 123

print(f’b:{b:<10}’)
print(f’b:{b:>10}’)
print(f’b:{b:=10}’)
print(f’b:{b:^10}’)

print('—')

c = -345

print(f’c:{c:<10}’)
print(f’c:{c:>10}’)
print(f’c:{c:=10}’)
print(f’c:{c:^10}’)``````
Execution results
``````a:ABC1234
a:   ABC1234
a: ABC1234
—
b:123
b:       123
b:       123
b:   123
—
c:-345
c:      -345
c:-      345
c:   -345``````

The “=" option seems to be effective when a sign is output. If [sign] is not specified, the sign is output only for negative numbers. Therefore, in the case of variable c, half-width spaces are output after the sign.

If you use “=" to specify [align] for a string, the following error will occur.

• ValueError: '=’ alignment not allowed in string format specifier

For this reason, we do not use “=" in the variable a in the sample program.

## Fill with zero and output

By specifying [fill], you can fill the rest of the specified number of display digits with the specified characters.

fill
When the number of digits displayed is greater than the number of digits in the value, the character that fills in the remaining portion.

In the following, we have tried to specify [fill] and [align].

Source
``````a = 'ABC1234′

print(f’a:{a:0<10}’)
print(f’a:{a:0>10}’)
print(f’a:{a:0^10}’)

print('—')

b = 123

print(f’b:{b:0<10}’)
print(f’b:{b:0>10}’)
print(f’b:{b:0=10}’)
print(f’b:{b:0^10}’)

print('—')

c = -345

print(f’c:{c:0<10}’)
print(f’c:{c:0>10}’)
print(f’c:{c:0=10}’)
print(f’c:{c:0^10}’)``````
Execution results
``````a:ABC1234000
a:000ABC1234
a:0ABC123400
—
b:1230000000
b:0000000123
b:0000000123
b:0001230000
—
c:-345000000
c:000000-345
c:-000000345
c:000-345000``````

In the above sample program, 0 is specified for [fill]. Therefore, 0 is output for the rest of the digits.

When the type of the variable is a string, it seems that [align] cannot be omitted. In the above output of variable a, when I wrote print(f’a:{a:010}’), the following error was printed at runtime.

• ValueError: '=’ alignment not allowed in string format specifier

When the variable is an integer type, it seems to be possible to write print(f’b:{b:010}’). However, the “0" in this case seems to be interpreted as “0" in [width].

Below, I tried omitting [align] for integer types.

Source
``````b = 123
c = -345
print(f’b:{b:010}’)
print(f’c:{c:010}’)``````
Execution results
``````b:0000000123
c:-000000345``````

In the case of negative numbers, the value of [align] seems to be set to “=" because the sign is filled with the character specified by [fill] after it.

## Output the sign

By specifying [sign], it is possible to output the sign of a positive number.

sign
The following symbols should be used according to the intended use:

Symbol Meaning
+ For positive and negative numbers, the sign is output.
If the number is negative, the sign is output.
half-width space If the number is positive, a half-width space is output first. If the number is negative, the sign is output.

I used the following sample program to check the display results.

Source
``````b = 123
c = -345
print(f’b:{b:+010}’)
print(f’c:{c:+010}’)

print(f’b:{b:-010}’)
print(f’c:{c:-010}’)

print(f’b:{b: 010}’)
print(f’c:{c: 010}’)``````
Execution results
``````b:+000000123
c:-000000345
b:0000000123
c:-000000345
b: 000000123
c:-000000345``````

When half-width space is specified for [sign], the first half-width space is printed in the display of positive numbers, as shown above. If you want to arrange the display of positive and negative numbers, it may be easier to see them if you specify a half-width space.

Note that the following error occurred when [sign] was specified for strings.

• ValueError: Sign not allowed in string format specifier

That’s all. I hope this is helpful to you.

Sponsored Links