LoginSignup
0
3

More than 3 years have passed since last update.

pythonの文字列に変数を埋め込む方法

Last updated at Posted at 2020-09-23

pythonの文字列に変数を埋め込む方法

・ここでは、2つ紹介します。

・1,f-string
・2,format()

f-string

先頭にfをつけて定義する文字列

a="apple"
f"I like an {a}"#変数の値で置換
実行結果
I like an apple

format()

文字列内の{}が、メソッドstr.format()の引数に渡した値に置換される。

a="apple"
"I like an {}".format(a)
実行結果
I like an apple

複数の引数を置換する

・1.f-string

a="apple"
b="orange"
f"I like an {a} and an {b}"
実行結果
I like an apple and an orange

2.format()

a="apple"
b="orange"
"I like an {} and {}".format(a,b)
実行結果
I like an apple and an orange
0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3