Home > database >  How to create a variable name that includes a DateTime value
How to create a variable name that includes a DateTime value

Time:12-25

I'm writing a function that declares 3 variables for each day (morning,midday,evening). I want these to be essentially named something like: 'morning_241222' etc.

I don't know how to automate variable declaration to have unique naming in Python 3 as well as how to create variable names that incorporating values from other places.

it obviously didn't work, but I tried:

today_%d%m%y = "test"

CodePudding user response:

you can tray vars() to assign string to variable name. this code works:

myVar = vars()
myVar['today_{}'.format("241222")] = 20
print(today_241222)

output will be:

20
  • Related