Search code examples
pythonpython-3.xvariable-variables

How do I split and reconstruct a variable name while holding its original value


Is it possible to split variables that have already been assigned values, and re-piece them back together to hold those same previous values?

For Example:

URLs.QA.Signin = 'https://qa.test.com'
TestEnvironment = 'QA'
CurrentURL = 'URLs.' + TestEnvironment + '.Signin'
print(CurrentURL)

Outputs as: 'URLs.QA.Signin'

but I would like it to:

Output as: 'https://qa.test.com'

The purpose is so I can plug in any value to my 'TestEnvironment' variable and thus access any of my massive list of URL's with ease =P

I am green with Python. Your time and efforts are greatly appreciated! =)


Based upon evanrelf's answer, I tried and loved the following code!:

This is exactly what i'm looking for, I might be over complicating it, any suggestions to clean up the code?

urls = {}
environment = 'qa'
district = 'pleasanthill'
url = environment + district
urls[url] = 'https://' + environment + '.' + district + '.test.com'
print(urls[url])

Output is: https://qa.pleasanthill.test.com


Solution

  • I would recommend you look into Python's dictionaries.

    urls = {}
    urls['qa'] = 'https://qa.test.com'
    
    test_environment = 'qa'
    print(urls[test_environment])
    // => https://qa.test.com