How do you convert string as a variable name in python?



How do you convert string as a variable name in python?..

Answer / Nirbhay Narayan Pandey

In Python, you can use the 'globals()' or 'locals()' functions to get a dictionary of current global and local symbol table (variable names). Then, you can use the dictionary to create or access variables using strings. Here's an example:

my_var = 10
var_name = 'my_var'
print(globals()[var_name]) # Outputs: 10

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Python Interview Questions

Explain Inheritance in Python?

1 Answers  


What happens if an error occurs that is not handled in the except block?

1 Answers  


What are the steps required to make a script executable on Unix?

1 Answers  


What is the optional statement used in a try except statement in Python?

1 Answers  


What does sorted do in python?

1 Answers  


What are variables in python?

1 Answers  


How many basic types of functions are available in python?

1 Answers  


What is flask- wtf? Explain its features.

1 Answers  


What is range() in python?

1 Answers  


What is the statement that can be used in python if the program requires no action but requires it syntactically?

1 Answers  


Tell me the use of // operator in python?

1 Answers  


Does python have polymorphism?

1 Answers  


Categories