Edward N. Edward N. - 1 year ago 99
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

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 Source

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]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download