10 lines
912 B
Markdown
10 lines
912 B
Markdown
# 20240323192523 pythonのf-string
|
||
#python
|
||
## 基本的な使いかた
|
||
`{}`をplacefolderとして文字列に
|
||
"{} {} {}".format(a, b, c) とかやって変数を埋めこめる機能。 [rubyの式展開](https://docs.ruby-lang.org/ja/latest/doc/spec=2fliteral.html#exp) 、[javascriptの テンプレートリテラル](https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Template_literals)などにも近いが、
|
||
細かい書式指定なども可能なため、Cの[printfのフォーマット指定](https://ja.wikipedia.org/wiki/Printf#%E6%9B%B8%E5%BC%8F%E5%8C%96%E6%96%87%E5%AD%97%E5%88%97)が一番近いかもしれない。といいつつ、現状だとそこまで色々使っていないかも。
|
||
参考サイトの説明が詳しい。
|
||
|
||
## Ref.
|
||
- [Pythonのf文字列(フォーマット済み文字列リテラル)の使い方 | note.nkmk.me](https://note.nkmk.me/python-f-strings/)
|