String format 2
tmp1.formt(‘Ham’, 200) ↓ Ham 200
In addition, as an example of format specification, write an example of format specification for displaying percentages. "12708" is the total population of Japan at the end of 2015, and "6381" is the number of employees (both units are 10,000). The working population ratio is displayed as a percentage, displaying two digits after the decimal point. In the second example, we put a comma in place of 1000. [Specify notation and insert] “{:.2%}”.format(6381/12708) ↓ 50.21%
“{:,}”.format(10000) ↓ 10,000
You can also specify the type of the replacement character string by putting alphabetic characters such as "c" and "d" at the end of the format specification part. "C" embeds the element as a character and "d" as a decimal integer.
[Format () option list]
option | Description |
---|---|
< | Fill in the spaces so that the elements are left-justified.{:<10}Use like. You can specify supplementary characters by putting a symbol etc. in front of the option. |
> | Fill in the spaces so that the elements appear right-justified.{:>10}Use like. You can specify supplementary characters by putting a symbol etc. in front of the option. |
^ | Make up for space so that the element is in the center.{:^20}Use like. You can specify supplementary characters by putting a symbol etc. in front of the option. |
+ | Sign the number. ”{:+}”.format(10)Is "+To 10,"{:+}”.format(-10)Is "-It will be "10". |
- | Sign only when the number is negative. ”{:-}”.format(10)To "10", "{:-}”.format(-10)Is "-It will be "10". |
Blank | When the number is positive, it is marked with a blank, and when it is negative, it is marked with a sign. ”{: }”.format(10)To "10", "{: }”.format(-10)Is "-It will be "10". |
c | Embed the element as a string. |
d | Embed the element as a decimal integer. An error will occur if the element to be replaced is a number containing a decimal point or a character string. |
f | Embed the element as a decimal integer. It can handle numbers including a decimal point.{:.2f}You can specify the precision after the decimal point by doing. |
x | Embed the element as a hexadecimal string such as "1f4e". The alphabetic part will be lowercase. If you use an uppercase X instead of x, the alphabetic part will be capitalized. |
b | Embed the element as a binary string such as "0110". |
% | Embed the element as a percentage.{:.1%}You can specify the precision after the decimal point by doing. |
, | Embed with a comma in the 1000s place of the number. |
Recommended Posts