Search code examples
pythonconcatenationnaming

How do I name an object in python by concatenating two strings?


I want to name an object "Nov2019" and I am trying the following:

Month = "Nov"
Year = "2019"
Year + Month = [100, 90, 80]

I want to have a list containing three integers which is named "Nov2019" by concatenation. Is it possible?


Solution

  • The simplest way is to wrap Year + Month with locals():

    Month = "Nov"
    Year = "2019"
    locals()[Year + Month] = [100, 90, 80]
    
    print(Nov2019)
    

    Output:

    [100, 90, 80]
    

    Note: Using locals(), vars(), globals(), eval(), exec(), etc. are all bad practices.
    Best to go with a dict.