PDA

View Full Version : Python: Is it possible to site a string with in a string?



BlackRat90
October 8th, 2010, 12:22 AM
I want to be able to have a string with in a string.

Example:

food = ['Mexican','Italian']
Mexican = ['Tacos','Burritos']

print "My favorite food is:", food[0]

OUTPUT:

My favorite food is: ['Tacos','Burritos']



Is it possible to do this??

Queue29
October 8th, 2010, 12:34 AM
Take a look at the dict type.

http://docs.python.org/library/stdtypes.html#dict

epicoder
October 8th, 2010, 01:55 AM
This is possible, and is called nesting. Instead of a string, you can use a variable name to include it in a list. To use your example:


Mexican = ['Tacos','Burritos']
food = [Mexican,'Italian']

print "My favorite food is:", food[0]
OUTPUT:

My favorite food is: ['Tacos', 'Burritos']

Note the lack of quotes around Mexican in the food variable.

nvteighen
October 8th, 2010, 07:15 AM
I'd go for a dictionary here. It's much clearer and explicit and allows to do some nice things. Look at this:



#!/usr/bin/env python

def main():
food = {"mexican" : ["Tacos", "Burritos"],
"italian" : ["Pizza", "Spaghetti", "Stromboli"]}

choice = raw_input("What's your favorite food? ")

# choice.lower() returns your input in lowercase... that makes this program
# case non-sensitive.
print("Your favorite food is: %s" % food[choice.lower()])

if __name__ == "__main__": # Prevents execution when importing this module
main()


A dict is like an array, but instead of accesing the items by index, you do it by key. So, using strings as keys allows you to ask for input and use the very same input as the key for the data you want.