public_notes/content/20240323192523 pythonのf-string.md

11 lines
912 B
Markdown
Raw Permalink Normal View History

2024-03-23 20:15:20 +09:00
# 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/)