0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Python 文字列の中で変数を展開する formatとfstring

Posted at

f = "hoge"
l = "ホゲ男"

# "{}"置換フィールドに入れる文字列を指定
print('First={} Last={}'.format(f,l)) # First=hoge Last=ホゲ男

# 2 "{}" 置換フィールドを引数番号指定
# .formatカッコ内は左から(0,1,2,3....)という順番になる。
print('First={0} Last={1}'.format(f,l)) # First=hoge Last=ホゲ男
# 引数番号は自由に変更が可能
print('First={1} Last={0}'.format(f,l)) # First=ホゲ男 Last=hoge

# 3 変数を指定した展開
print('First={first} Last={last}'.format(first=f,last=l)) # First=ホゲ男 Last=hoge

# 4 新しいバージョン python3.6以降 処理が早い
print(f'First={f} Last={l}') # First=hoge Last=ホゲ男

参考にした記事
Pythonの文字列の中で変数を展開する方法

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?