f文字列(Python)

Pythonにおいて、f文字列とは、文字列(Python)内に直接変数(Python)や式の値を埋め込むための方法のことです。Python 3.6以降で利用可能です。

f文字列を使用するには、文字列(Python)の前に「f」または「F」を付けます。そして、波括弧 { } を使って、直接変数(Python)や式を文字列(Python)内に埋め込みます。

name = "Alice"
age = 30
message = f"Hello, {name}. You are {age} years old."
print(message) # Hello, Alice. You are 30 years old.

x = 10
y = 5
result = f"{x} times {y} is {x * y}"
print(result) # 10 times 5 is 50