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 1 year has passed since last update.

Python 文字列の中に変数値を入れる

Posted at

文字列の中に変数値を入れる

フォルダやファイル作成時に今日の日付を入れたり時にformatを使い実施

import datetime as dt
import os

today = dt.date.today().strftime('%Y%M%d')
today
'20220012'

これでフォルダ名などの文字列に追加したい日付文字列が作成できたので、フォーマットを使って文字列と結合します。
コーテーションの前にfをつけて作成します。(Python3.6以降)
もちろん+で結合することも可能ですが、フォーマットを使ったほうがコードが見やすいと思います。

print(f'newfolder_{today}make')
print('newfolder_' + today + 'make')
newfolder_20220012make
newfolder_20220012make

これを使えばフォルダ名に作成日を追加することが可能

os.makedirs(f'newfolder_{today}make')
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?