2
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.

format関数にキーワード引数を段階的に指定する小技

Posted at

format関数にキーワード引数を段階的に指定する小技

検索で見つからなかったので、投稿しておきます。

  • format関数にキーワード引数を指定
filename = 'epoch_{epoch}_id_{id}.png'

epoch=100
id=1000

filename2 = filename.format(epoch=epoch, id=id)
print('filename2 : ' + filename2)
filename2 : epoch_100_id_1000.png
  • キーワード引数を一部しか指定しないと...
filename2 = filename.format(epoch=epoch)
KeyError                                  Traceback (most recent call last)
<ipython-input-3-62421f7ed55c> in <module>()
----> 1 filename2 = filename.format(epoch=epoch)

KeyError: 'id'
  • キーワード引数を段階的に指定するには...

指定しない段階では、key1='{key1}'と指定する。

filename2 = filename.format(epoch=epoch, id='{id}')
print('filename2 : ' + filename2)

filename3 = filename2.format(id=id)
print('filename3 : ' + filename3)
filename2 : epoch_100_id_{id}.png
filename3 : epoch_100_id_1000.png
2
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
2
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?