Example (basic form)
'{}The weather at the station is sunny'.format('Shibuya')
#output
#'The weather at Shibuya station is sunny'
Example (no index number)
'{}The weather at the station is sunny'.format('Shibuya','Shinjuku','Mejiro','Ikebukuro')
#output
#'The weather at Shibuya station is sunny'
Example (index number 0)
'{0}The weather at the station is sunny'.format('Shibuya','Shinjuku','Mejiro','Ikebukuro')
#output
#'The weather at Shibuya station is sunny'
Example (index number 1)
'{1}The weather at the station is sunny'.format('Shibuya','Shinjuku','Mejiro','Ikebukuro')
#output
#'The weather at Shinjuku station is sunny'
Example (index number 4)
'{4}The weather at the station is sunny'.format('Shibuya','Shinjuku','Mejiro','Ikebukuro')
#output(error)
#IndexError: Replacement index 4 out of range for positional args tuple
If there is no data corresponding to the index number, an error will occur.
Example (call one argument multiple times)
'{0}Weather.{0}Ward,{0}The weather at the station is sunny'.format('Shibuya')
#output
#'Shibuya weather. The weather at Shibuya Ward and Shibuya Station is sunny'
If 0 is not specified for the index number, an error will occur. Because the index number in {} is automatically set in order from 0.
Example (error case)
'{}Weather.{}Ward,{}The weather at the station is sunny'.format('Shibuya')
#output(error)
#IndexError: Replacement index 1 out of range for positional args tuple
Example (multiple arguments, no number specified)
'{}Weather.{}Ward,{}The weather at the station is sunny'.format('AAAA','BBBB','CCCC','DDDD')
#output
#'AAAA weather. The weather at BBBB Ward and CCCC Station is sunny'
Index number is set automatically --First {} = {0} --Second {} = {1} --Third {} = {2} ・ ・ ・
Example (multiple arguments, number specified ①)
'{0}Weather.{1}Ward,{2}The weather at the station is sunny'.format('AAAA','BBBB','CCCC','DDDD')
#output
#'AAAA weather. The weather at BBBB Ward and CCCC Station is sunny'
The result is the same as without a number.
Example (multiple arguments, number specified ②)
'{3}Weather.{2}Ward,{2}The weather at the station is sunny'.format('AAAA','BBBB','CCCC','DDDD')
#output
#'DDDD weather. The weather at CCCC Ward and CCCC Station is sunny'
・ In no particular order ・ The same number can be set multiple times
Example (set by character string, numerical value, variable)
mountain = 'Mt. Kitadake'
altitude = '3193'
'{}so{}The highest mountain{}.. The altitude is{}m'.format('Japan',2,mountain,altitude)
#output
#'The second highest mountain in Japan is Mt. Kitadake. Altitude is 3193m'
・ Character string: Japan ・ Numerical value: 2 -Variable (character string): mountain ・ Variable (numerical value): altitude
Example (width setting)
'{:2}Set gap in format'.format('A')
#output"'Set the gap in A format'」
-Argument "A": 1 byte -Width setting: 2 bytes ⇒ 2 bytes wide (1 byte gap)
Example (width setting "argument is larger")
'{:2}Set gap in format'.format('AAAAA')
#output"'Set gaps in AAAAA format'」
-Argument "AAAAA": 5 bytes -Width setting: 2 bytes ⇒ 5 bytes wide
Example (width setting "width 0")
'{:0}Set gap in format'.format('A')
#output(error)
#「ValueError: '=' alignment not allowed in string format specifier」
symbol | Placement |
---|---|
< | Left justified |
^ | Centered |
> | Right justified |
** How to use **
{<n}
.format()
└ "<": Left justified. a line sign
└ "n": width (integer greater than or equal to 0)
Example (left justified)
'{:<10}Set gap in format'.format('AAA')
#output
#「'Set gaps in AAA format'」
Example (centered)
'{:^10}Set gap in format'.format('AAA')
#output
#「'Set gaps in AAA format'」
Example (right justified)
'{:>10}Set gap in format'.format('AAA')
#output
#「'Set gaps in AAA format'」
Example (set the placement individually)
'The first one"{:<7}". The second "{:^7}". Third "{:>7}」'.format('AAA','BBB',333)
#output
# 'The first is "AAA". The second "BBB". The third "333"'
{0<n}.format()
└ "0": Fill with 0
└ "<": Left justified. Placement specification
Fill with zero(Left justified)
'「{:0<10}Fill the gap with zero'.format('AAA')
#output
# '"AAA0000000" Fill the gap with zero'
Fill with zero(Centered)
'「{:0^10}Fill the gap with zero'.format('AAA')
#output
# '"000AAA0000" Fill the gap with zero'
If the gap is odd, there will be more behind.
Fill with zero(Right justified)
'「{:0>10}Fill the gap with zero'.format('AAA')
#output
# '"0000000 AAA" Fill the gap with zero'
@Fill with(Left justified)
'「{:@<10}Fill the gap'.format('AAA')
#output
# '「AAA@@@@@@@Fill the gap'
Fill with ★(Centered)
'「{:★^10}Fill the gap'.format('AAA')
#output
# '"★★★ AAA ★★★★" Fill the gap'
Fill with the number 5(Right justified)
'「{:5<10}Fill the gap with zero'.format('AAA')
#output
# '"AAA555 5555" Fill the gap with zero'
Character string of 2 digits or more (error)
'「{:★★^10}Fill the gap with zero'.format('AAA')
#Output (error)
# ValueError: Invalid format specifier
Two or more digits (error)
'「{:11<10}Fill the gap with zero'.format('AAA')
#Output (error)
# ValueError: Invalid format specifier
Example (comma in thousands)
'{:,}'.format(123456789)
#output
# '123,456,789'
%display(Default)
'{:%}'.format(1)
#output
# '100.000000%'
%display(小数点以下非display)
'{:.0%}'.format(1)
#output
# '100%'
%display(2 digits after the decimal point)
'{:.2%}'.format(1)
#output
# '100.00%'
%display(15 digits after the decimal point)
'{:.15%}'.format(1)
#output
# '100.000000000000000%'
The default of 6 digits or more can be set.
The default is a minus sign only. └ Same as'{:-}'(no setting required)
Add a + sign
'The first"{:+}". The second "{:+}". Third "{:+}」'.format(-500, 300, 2.56)
#output
# 'The first"-500 ". The second "+300 ". Third "+2.56」'
Decimal point(Default)
'{}'.format(1.23)
#output
# '1.23'
Decimal point(2 digits)
'{:.2f}'.format(1.23456789)
#output
# '1.23'
Decimal point(0 digits)
'{:.0f}'.format(1.234345678)
#output
# '1'
Decimal point(12 digits)
'{:.12f}'.format(1.234345678)
#output
# '1.234345678000'
Fixed point (default)
'{:e}'.format(123456789)
#output
# '1.234568e+08'
Fixed point (2 digits)
'{:.2e}'.format(123456789)
#output
# '1.23e+08'
Fixed point (no decimal point)
'{:.0e}'.format(123456789)
#output
# '1e+08'
Fixed point (argument is decimal)
'{:.0e}'.format(123.456789)
#output
# '1e+02'
Specify a variable in the argument
'{a}so{b}The highest mountain{c}。'.format(a='Japan',b=2,c='Mt. Kitadake')
#output
# 'The second highest mountain in Japan is Mt. Kitadake.'
Variables and strings are in error
'{a}so{b}The highest mountain{2}。'.format(a='Japan',b=2,Mt. Kitadake)
#output(error)
# SyntaxError: positional argument follows keyword argument
Variables and numbers are errors
'{a}so{1}The highest mountain{c}。'.format(a='Japan',2,c='Mt. Kitadake')
#output(error)
# SyntaxError: positional argument follows keyword argument
Cannot be specified by index number
'{0}so{1}The highest mountain{2}。'.format(a='Japan',b=2,c='Mt. Kitadake')
#output(error)
# IndexError: Replacement index 0 out of range for positional args tuple
NG without index number specification
'{}so{}The highest mountain{}。'.format(a='Japan',b=2,c='Mt. Kitadake')
#output(error)
# IndexError: Replacement index 0 out of range for positional args tuple
datetime.date type
import datetime as dt
past = dt.date(2017,1,3)
"{0:%Y year%#m month%#d day}".format(past)
#output
# "{0:%Y year%#m month%#d day}".format(past)
datetime.datetime type
import datetime as dt
today = dt.datetime.now()
"{0:%Y year%m month%d day---%I time%M minutes%S seconds}".format(today)
#output
# 'March 20, 2020---02:08:39'
Extract individually(Month and second)
import datetime as dt
today = dt.datetime.now()
"{0:%#m month,%S seconds}".format(today)
#output
# 'March, 52 seconds'
Reference: [How to change the date format](https://qiita.com/yuta-38/items/337059e1eafab3582851#2-2format%E3%83%A1%E3%82%BD%E3%83%83] % E3% 83% 89% E3% 81% A7% E5% A4% 89% E6% 9B% B4% E3% 81% 99% E3% 82% 8B)
Recommended Posts