No. Use a dictionary.
>>> names = {'p1': 100, 'p2': 200}
>>> t = names['p1']
>>> t
100
This will throw a KeyError if the name does not exist:
>>> t = names['p3']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'p3'
... but you can use dict.get and provide a default value:
>>> t = names.get('p3', 'default')
>>> t
'default'
where the default-default-value is None.
(By the way, this has nothing to do with converting strings to a char(acter?), your terminology is quite confusing.)
Answer from timgeb on Stack OverflowNo. Use a dictionary.
>>> names = {'p1': 100, 'p2': 200}
>>> t = names['p1']
>>> t
100
This will throw a KeyError if the name does not exist:
>>> t = names['p3']
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
KeyError: 'p3'
... but you can use dict.get and provide a default value:
>>> t = names.get('p3', 'default')
>>> t
'default'
where the default-default-value is None.
(By the way, this has nothing to do with converting strings to a char(acter?), your terminology is quite confusing.)
You're probably looking for eval(): https://docs.python.org/3.5/library/functions.html#eval
p1 = 100
print(eval('p1')) # prints 100
If you want to evaluate expressions you can also use exec()
p1 = 100
t = 'p1 += 99'
exec(t)
print(p1) # prints 199
But as other already pointed out, you should avoid it if possible. timgeb provided a nice alternative with dictionaries for example.
If you want to turn a string into a global variable you could also do:
globals()['y'] = 5
print(y)
Note: when you run your code in a global scope (outside a function), you can also use locals()['y'] = 5 - but this will not work in a non-global scope!
python - Convert a list of characters into a string - Stack Overflow
python into to char to string coversion - Stack Overflow
How do i type in {, }, " characters into a string?
Converting a lists of characters into a list of strings
What is the easiest way for string to char in Python?
Can I convert string to char array in Python with tuple instead of list?
Can I access string as char without conversion in Python?
Use the join method of the empty string to join all of the strings together with the empty string in between, like so:
>>> a = ['a', 'b', 'c', 'd']
>>> ''.join(a)
'abcd'
This works in many popular languages like JavaScript and Ruby, why not in Python?
>>> ['a', 'b', 'c'].join('')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'list' object has no attribute 'join'
Strange enough, in Python the join method is on the str class:
# this is the Python way
"".join(['a','b','c','d'])
Why join is not a method in the list object like in JavaScript or other popular script languages? It is one example of how the Python community thinks. Since join is returning a string, it should be placed in the string class, not on the list class, so the str.join(list) method means: join the list into a new string using str as a separator (in this case str is an empty string).
Somehow I got to love this way of thinking after a while. I can complain about a lot of things in Python design, but not about its coherence.