Cant print an variable associated to a string? [duplicate] - python

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.

Related

Can we use a variable to create a new variable in Python? [duplicate]

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}"

Execute Statement in Python error Name Not Defined [duplicate]

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

Python Passing list by its name to a function [duplicate]

This question already has answers here:
How can I select a variable by (string) name?
(5 answers)
Closed 8 months ago.
names=['abcd','efgh']
nameoflist='names'
def(nameoflist=[]):
return nameoflist
I want to be able to return the entire list from the function
Assuming names is global as specified in the question, you can do this
names=['abcd','efgh']
nameoflist='names'
def return_names(nameoflist):
return globals()[nameoflist]
However, this is pretty ugly, and I'd probably try another way to do it. What do you need the name for? Is there any other way to get the information you're asking for?
This one way to do what you are asking. But it is not good programming.
names=['abcd','efgh']
def list_by_name(list_name):
return eval(list_name)
print(list_by_name('names'))
Also, argument list_name should be a string. It should not default to a list, which would make the function to fail if called without argument. It should not have a default value.

Is it possible to design a function from input() and assign a variable to that function call? [duplicate]

This question already has answers here:
How do I get a result (output) from a function? How can I use the result later?
(4 answers)
Closed 6 years ago.
Sorry for the title but I'm not sure how else to word it. Anyway I'm just starting to learn Python and ran into this problem. I'm trying to assign a variable to a function call, where the function contains input()
Unfortunately this never assigns anything to the variable a
def question(letter):
input(letter + '? ')
a = question('a')
print(a)
So I guess my real question is, why doesn't that work? Why doesn't it assign the user input to the variable a?
Thanks
You need to return the user input. Right now the user input is captured nowhere.
def question(letter):
return input(letter + '? ')
a = question('a')
print(a)

How can I turn a regular string into a keyword? [duplicate]

This question already has answers here:
Converting String into Object Python
(2 answers)
Closed 9 years ago.
Recently, I have been programming in the language of Python. I've came across the problem of trying to convert a string into a keyword.
Suppose I use a raw_input() phrase and turn that string into an object, list, or dictionary.
For example, I can turn the string "Foo" into Foo and assign that name to a python structure.
How would I do this?
You don't want to do that, so instead of doing it, use a dictionary:
answer = raw_input("Enter: ") # Let's assume I enter "Foo"
mydict = {answer: raw_input("Enter a value for {} ".format(answer)} Let's say I enter "5"
print mydict.get('Foo')
# 5
You can modify the globals dictionary if you wish.
globals()[raw_input()] = None # whatever "structure" you desire.
print foo
The above code would only work if the user actually inputted the string 'foo'. Otherwise it would say NameError: name 'foo' is not defined.
But yeah like Haidro said, whatever you're trying to do this for is probably a silly idea.
As #icktoofay noted in the comments, this will only work with the return value of the built-in function globals(). Modifying the return value of the built-in function locals() will not actually modify the local namespace.

Categories

Resources