LoginSignup
0
0

More than 1 year has passed since last update.

変数の値を含んだ文字列を作成したいときはf文字列を使うとよい

Posted at

やりたいこと

変数の値を含んだ文字列を作成したい

結論

f文字列を使用するとよい(Python3.6以上に限る)

方法1: +演算子を使用する

ソースコード

name = '太郎'
text = '私の名前は ' + name + ' です'

print(text)

出力

私の名前は 太郎 です

方法2: format()を使用する

ソースコード

name = '太郎'
text = '私の名前は {} です'.format(name)

print(text)

出力

私の名前は 太郎 です

方法3: f文字列を使用する

ソースコード

name = '太郎'
text = f'私の名前は {name} です'

print(text)

出力

私の名前は 太郎 です

参考

0
0
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
0