This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 4 years ago.
I am trying to use a Python list comprehension to get the Variable Name SW1 to equal the string "sw1" but I keep getting an error saying sw1 not defined
VarList = ["SW1"]
VarListEnd = ["sw1"]
list3 = [exec("%s="%x + "%s"%y) for x in VarList for y in VarListEnd]
list3
How do I amend the exec statement because that is where I think the error is?
Really appreciate your help. Thanks in advance.
You don't need exec for that. To create variables, you can do
for name, value in zip(VarList, VarListEnd):
locals()[name] = value
Related
This question already has answers here:
Which is the preferred way to concatenate a string in Python? [duplicate]
(12 answers)
Closed 5 months ago.
I am wondering if we can use a variable name in order to create a new variable, for example:
Let's assume I have this variable:
Var = 'Jim'
Lets say I want to concatenate the variable with a string, in this case the string is the word Mr:
NewVar = "String"Var
So that if I print the new variable, the output would look something like:
MrJim
This can be achieved in bash like this:
NewVar=Mr${Var}
But I have not found a way to do this in Python. Please let me know if you know how to do it.
Have a look at python string interpolation.
var = "Jim"
new_var = f"Mr {var}"
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 1 year ago.
x=['a','b','c']
y=['d','e','f']
z=['g','h','i']
string='x'
#Now I would like to somehow get the list printed or returned by only using the string variable.
Use globals or locals:
x=['a','b','c']
y=['d','e','f']
z=['g','h','i']
string='x'
print(globals()[string])
['a','b','c']
If you have multiple list and you want to print them based on some key it's best to use a dictionary.
lstDic = {
"x":['a','b','c'],
"y":['d','e','f'],
"y":['g','h','i']
}
string='x'
print(lstDic[string])
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 2 years ago.
I am currently trying to define a number of variables as empty lists (equal in quantity to the number of iterations of the loop). So far I've tried the following:
for i in [1,2,3]:
locals()["temp_set_" + str(i)] = []
When I try to print(temp_set_1) outside of the loop in order to verify the output, I get the following error: NameError: global name 'temp_set_1' is not defined
I am not sure what I'm doing wrong at the moment and would appreciate it if someone could point me in the right direction.
In my mac, with python3.6, it works.
for i in [1,2,3]:
locals()["temp_set_" + str(i)] = []
This question already has answers here:
How can I create multiple variables from a list of strings? [duplicate]
(2 answers)
Closed 5 years ago.
I couldn't find an answer for this, so can you give a beginner pythonist some help. I have a string and if it is in globals, then I want to use that variable to manipulate it. I get an error that I'm using string to append, when my global variable is a list.
color = []
keyword = "color"
if keyword in globals():
keyword.append("testing")
globals() returns a dictionary, which you can use like any other dictionary
globals()[keyword] = <my_new_value>
This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 6 years ago.
i was playing around with python and was wondering why i cant print a variable thats associated with a string of text, this is what i wrote.
name = ("martim")
print name
Thanks for your help!
I hope these few lines will help you to understand how to use variables, values and print-statement in Python 2:
>>> name = "martin"
>>> print name
martin
>>> print "name"
name
>>> print "martin"
martin
Also you can take a look at the beginner's guide for Python: https://wiki.python.org/moin/BeginnersGuide
The proper way to perform what you're attempting to do is (note the comments are just to help understanding):
# Set name to "martin"
name = "martin"
# Print the value of name
print(name)
When assigning values to variables, parentheses shouldn't be used unless you're using a function for assignment; e.g., pi = float("3.14"). Print statements require parentheses as it is a function and the value being printed is the parameter.