Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How create variable name with other variable's value in it?

a = "Jack"
b = "Sam"

Now I want some way to create this:

c_{b value} = 10 

That means:

c_Jack = 10

And the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

c_{a value} = 20 

That means:

c_Sam = 20

>Solution :

This is actually a bad practice to do this. You better to use a dict.

BTW, you can do this:

In [3]: a = "Jack"

In [4]: exec(f'c_{a} = 10')

In [5]: c_Jack
Out[5]: 10


Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading