
Table of Contents(目次)
Basic formatting
In this lecture, we will learn about string formatting.
The notation for formatting is as follows.
{var:formatting}
{:formatting}
(When using “positional arguments” in the format method)
First, enter a colon in curly brackets ({:}
). Next, enter the variable name (or, in the case of the format method, the “name” that specifies the replacement location) on the left side of the colon (var:
). However, when using the “positional arguments” of the format method, do not write anything on the left side of the colon. Finally, specify the format on the right side of the colon (:formatting
).
Format specifications are written in the same format for both the format method and f-strings.
The following code, which includes digit grouping format specifications, is valid and follows all the rules mentioned above.
# Output result: "1,234,567,890"
num = 1234567890
# === Formatting using the format method === #
# Use positional arguments (leave the left side of the colon blank)
formatted_num = '{:,}'.format(num)
# Use keyword arguments (curly_brackets_name=variable_name)
formatted_num = '{num:,}'.format(num=num)
# Use keyword arguments (curly_brackets_name≠variable_name)
formatted_num = '{var:,}'.format(var=num)
# === Formatting with f-strings === #
formatted_num = f'{num:,}' # In the case of f-strings, only this notation is used.
Combining format specifications and string concatenation results in the following code.
# Output result: "Nico has $50,000 in savings."
who = 'Nico'
num = 50000
# === Formatting using the format method === #
# Use positional arguments (leave the left side of the colon blank, or write only “{}” if there is no format specification)
formatted_num = '{} has ${:,} in savings.'.format(who, num)
# Use keyword arguments (curly_brackets_name=variable_name)
formatted_num = '{who} has ${num:,} in savings.'.format(who=who, num=num)
# Use keyword arguments (curly_brackets_name≠variable_name)
formatted_num = '{name} has ${money:,} in savings.'.format(name=who, money=num)
# === Formatting with f-strings === #
formatted_num = f'{who} has ${num:,} in savings.' # In the case of f-strings, only this notation is used.
Zero padding (0n)
This format fills with zeros before numbers to align the number of digits displayed.
This format is often used for ID numbers (“00425”), times (“09:05”), and file name revisions (“revenue_rev001.xlsm”).
The format is specified in the form 0n
. After the first 0
(zero), specify the number of digits. To make it 3 digits, write 03
, and to make it 15 digits, write 015
.
num = 6
formatted_num = '{:03}'.format(num) # Three-digit zero padding ("006")
formatted_num = f'{num:015}' # 15-digit zero padding ("000000000000006")
Thousands separator (,)
This format has appeared many times before, and it inserts commas (,
) every three digits in the integer part.
The format specification is written after a colon (:
) followed by,
.
num = 1234567890
formatted_num = '{:,}'.format(num) # "1,234,567,890"
formatted_num = f'{num:,}' # "1,234,567,890"
Number of digits after the decimal point (.nf)
You can align the number of decimal places using format specifications.
Format specifications are specified in the form .nf
. After the first dot(.
), specify the number of decimal places. Finally, add f
. For example, to specify 2 decimal places, use .2f
; for 15 decimal places, use .15f
.
If the number of digits after the decimal point in the original number is smaller than the number of digits specified in the format specification, zeros are padded.
num = 3.1415
formatted_num = '{:.2f}'.format(num) # Display up to two decimal places (“3.14”)
formatted_num = f'{num:.15f}' # Display up to 15 decimal places ("3.141500000000000")
Percentage (%)
Use this when you want to display a ratio as a percentage (%). The displayed value is automatically multiplied by 100, and “%” is added at the end.
Specify the format by writing %
after a colon (:
).
The default behavior for percentage notation is that only the sixth decimal place is displayed, even if the original number has more decimal places. Additionally, if the original number has fewer than six decimal places, the remaining positions are padded with zeros.
# Fill with zeros up to the sixth decimal place ("14.159000%")
num = 0.14159
formatted_num = '{:%}'.format(num)
# Display up to the sixth decimal place ("14.159265%")
num = 0.14159265358979323846
formatted_num = f'{num:%}'
Percentage notation can be combined with digit specification for decimal places to freely change the number of digits.
Inserting .0
before %
sets the number of decimal places to 0, and inserting .2
sets the number of decimal places to 2. (Note: When specifying the number of decimal places for percentages, use the notation .n%
. The notation f
learned in “Number of digits after the decimal point (.nf)” is not necessary.)
num = 0.14159265358979323846
formatted_num = '{:.0%}'.format(num) # "14%"
formatted_num = f'{num:.2%}' # "14.16%"
Combination of format specifications
Format specifications can be used in combination.
To combine comma separation for the integer part and specification of the number of digits after the decimal point, write ,.2f
. (Connect ,
and .2f
together.)
# Comma separation and two decimal places ("1,234,567.89")
num = 1234567.891234
formatted_num = f'{num:,.2f}'