I want to be able to have a string with in a string.
Is it possible to do this??Code:Example: food = ['Mexican','Italian'] Mexican = ['Tacos','Burritos'] print "My favorite food is:", food[0] OUTPUT: My favorite food is: ['Tacos','Burritos']
I want to be able to have a string with in a string.
Is it possible to do this??Code:Example: food = ['Mexican','Italian'] Mexican = ['Tacos','Burritos'] print "My favorite food is:", food[0] OUTPUT: My favorite food is: ['Tacos','Burritos']
Take a look at the dict type.
http://docs.python.org/library/stdtypes.html#dict
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:
OUTPUT:PHP Code:
Mexican = ['Tacos','Burritos']
food = [Mexican,'Italian']
print "My favorite food is:", food[0]
My favorite food is: ['Tacos', 'Burritos']
Note the lack of quotes around Mexican in the food variable.
Don't use W3Schools as a resource! (Inconsequential foul language at the jump)
Open Linux Forums (More foul language, but well worth it for the quality of support and good humor.)
If you want to discuss W3Schools, please PM me instead of posting.
I'd go for a dictionary here. It's much clearer and explicit and allows to do some nice things. Look at this:
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.Code:#!/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()
Bookmarks