[Tutor] Concatenating Strings into Variable Names?

Alan Gauld alan.gauld at blueyonder.co.uk
Tue Feb 3 01:31:08 EST 2004

> I'm trying to write my first program besides good old hello world.
> there a way to concatenate strings into a variable name?

There is, but it's nearly always the wrong thing to do.
Instead use a dictionary.

> Here's a
> brief example of what i'm trying to do:
options = {}
options['choiceA_option1']= 2.0
options['choiceA_option2']= 3.0

> choice=raw_input("Do you want choice A or B?")
> option=raw_input("Do you want option 1 or 2?")
> the_answer="choice" + choice + "_option" + option
> print options[the_answer]

There is another example of this at the end of the OOP topic
on my web site.


Alan G
Author of the Learn to Program web tutor

More information about the Tutor mailing list