Edward N. Edward N. - 2 months ago 9
Python Question

Q: How do I concatenate str + int to = an existing variable

I want to combine a str + int to equal a existing variable that I already defined. I tried looking up on how this can be done and I've only found how to concatenate a str and int together with

print "var%d" % currentIndex


what I have is currentIndex being the index number of the selection in a PyQt4 combo box. I related the index number of the combo box selection to a large file full of var0 - var30. Each one being a list of information that I want to pull on demand when ever currentIndex is changed.

var0 = [ "a", "b", "c", "d" ]
( user picks a selection from GUI comboBox )
print "var%d" % currentIndex
var0


It's not printing the list it's just printing the var0 as a string. How do I get the two to equal var0 the variable and not var0 the str?

Answer

You can print the list like this:

print eval("var%d" % currentIndex)

But I would suggest you to use a nested list rather than 30 variables:

var = [["a", "b", "c", "d"], ["e", "f"], ...]
print var[currentIndex]
Comments