f-string is a simple way to describe the format function.
Once you remember the shape, the basic description is the same as the format function.
table of contents
It has been available since python3.6 (released in December 2016).
It is called a "formatted string literal" on the official page.
Click here for details (http://docs.python.org/ja/3.6/reference/lexical_analysis.html#f-strings)
-The description is simpler with f-string. -The degree of freedom is slightly higher in the format function. └ (Many patterns can be said to be complicated)
・ Simple code ・ All specified by variables -Describe variables and formats as a set └ Easy to understand which variable is given what format
-You can directly specify a numerical value or a character string as the value to be assigned. ・ You can leave blank in {}. (Index number is automatically assigned) ・ Can be specified by index number
Same as format function except using "f"'". Pattern without formatting below ":"
The format setting method is the same as the format function. Click here for details (https://qiita.com/yuta-38/items/9a93eea10ccc7ac2c8ad)
"'My name is AAA. I'm 111 cm tall. My favorite food is BBB'"
■f-string
f-string
a='AAA'
b=111
c='BBB'
f'my name is{a}is. How tall are you{b}cm. What is your favorite food{c}'
#output
# 'My name is AAA. Height is 111 cm. Favorite food is BBB'
** ▼ format function ① **
.format('AAA', 111, 'BBB')
format function ①
'my name is{}is. How tall are you{}cm. What is your favorite food{}'.format('AAA', 111, 'BBB')
#output
# 'My name is AAA. Height is 111 cm. Favorite food is BBB'
It is applied in {} in the order described in () of format.
An error occurs if the number of {} is greater than the number of elements specified in format.
format function ②
a='AAA'
b=111
c='BBB'
'my name is{}is. How tall are you{}cm. What is your favorite food{}'.format(a, b, c)
#output
# 'My name is AAA. Height is 111 cm. Favorite food is BBB'
format function ③ number specification
a='AAA'
b=111
c='BBB'
'my name is{2}is. How tall are you{1}cm. What is your favorite food{0}'.format(c, b, a)
#output
# 'My name is AAA. Height is 111 cm. Favorite food is BBB'
format function ③ number specification
'my name is{name}is. How tall are you{height}cm. What is your favorite food{favorit}'.format(height='111', favorit='BBB', name='AAA')
#output
# 'My name is AAA. Height is 111 cm. Favorite food is BBB'
There are many patterns that can be specified by the format function, but it is delicate whether all are necessary when actually using it.
It seems that one f-string can handle it.
Formatting is the same as the format function. (Format function)
f'{a:^n}
└ Formatting after ":"
└ "^" center justified ("<" left justified, ">" right justified)
└ "n" integer. Indicate the gap by the number of bits
--The text placement is meaningless unless a gap is specified.
Right justified
a= 'AAA'
f'my name is{a:^9}is'
#output
# 'My name is AAA'
** ▼ Fill with 0 (0 padding) **
f'{a:0>n}
└ Add 0 before the element placement symbol (here ">")
0 fill
a= 'AAA'
f'my name is{a:0>9}is'
#output
# 'My name is 000000 AAA'
** ▼ Fill with arbitrary characters **
f'{a:Z>n}
└ Insert the character you want to fill before the element placement symbol (here, ">")
└ Symbols, letters and numbers are OK
└ Only one character.
Fill with arbitrary characters
a= 'AAA'
f'my name is{a:e>9}is'
#output
# 'My name is yeah yeah yeah AAA'
f'{A:% Y year% # m month% # d day}'
└ “A” Variable containing date data
└ Describe the date format under ":"
└ "% Y" 4 digits in the Christian era
└ Month without "% # m" 0 ("% -m" for Mac)
└ "% # d" month without 0 ("% -d" for Mac)
For the types of specifiers (% Y,% m, etc.) [here](https://qiita.com/yuta-38/items/ba6dce967ede22e37c60#%E6%97%A5%E4%BB%98%E3% 81% AE% E6% 8C% 87% E5% AE% 9A% E5% AD% 90% E4% B8% 80% E8% A6% A7)
Date formatting
import datetime as dt
past = dt.date(2017,1,3) #datetime.date(2017, 1, 3)
f'{past:%Y year%#m month%#d day}'
#output
# f'{past:%Y year%#m month%#d day}'
Since variables and formats are set like {variable: format}, it is easy to understand what format is set for what.
In the format function, visibility is reduced because it is specified by the index number.
Format the date with the format function
import datetime as dt
past = dt.date(2017,1,3)
now = dt.date(2020,1,28)
future = dt.date(2022,1,30)
"In the old days{0:%Y year%#m month%#d day}.. now{1:%Y/%#m/%#d}.. The future is{2:%y%m%d}。".format(past, now, future)
[Click here for details](https://qiita.com/yuta-38/items/9a93eea10ccc7ac2c8ad#format%E9%96%A2%E6%95%B0%E3%81%A7%E3%81%A7%E3% 81% 8D% E3% 82% 8B% E3% 81% 93% E3% 81% A8)