-1

How should I do if I want to output "name[0] = 6" instead of "t"?

enter image description here

guorui
  • 871
  • 2
  • 9
  • 21
  • 1
    please read content submission rules. You should be using formatting tools to provide us your code instead of pasting a screenshot of the code. – Simas Joneliunas Jul 03 '18 at 07:42
  • Please do not post code as images – U13-Forward Jul 03 '18 at 07:42
  • The result of `print name` is `table2`. – guorui Jul 03 '18 at 07:44
  • Please post code as just that code, not as an image. See [mcve]. – Bugs Jul 03 '18 at 07:45
  • This has nothing to do with "variable variables". This man just want to print the name of the variable. – Florin Ghita Jul 03 '18 at 07:49
  • @FlorinGhita I am fairly certain the OP wants `name` to reference `table2`. IOW, they want to dynamically execute a string as Python code, and that is the canonical duplicate, despite its unfortunate title. The answer is the same, though, which is that you can use `eval` to do this, but you almost certainly shouldn't. If you disagree, feel free to vote to re-open, or perhaps the OP can clarify. – juanpa.arrivillaga Jul 03 '18 at 08:05
  • Yes, he want a dynamic code and you're right, generating code is a bad ideea. – Florin Ghita Jul 03 '18 at 08:11

2 Answers2

0

print "name[0] = "+name[0] for python 2 #concatenation

print ("name[0] = "+name[0]) for python 3

Taohidul Islam
  • 5,246
  • 3
  • 26
  • 39
0

You should try this:

print("name[0] = 6") # will work on python2 and python3

or

print "name[0] = 6" # only in python2

This will make python think that it is just a string, not a variable.

teoman
  • 959
  • 2
  • 10
  • 21