subtracting from strings
So I have a string called "mainString1" and it has a list consisting of "pearls diamonds jewels rocks" I have another that contains the input from the user called "enteredList" and I want to subtract anything from "mainString1" that is entered by the user so if they entered "pearls" then it prints "diamonds jewels rocks"
I'm not sure of all that is wrong with the following
Also how would I count to make it read "You still need 3 of the following" or change the number to the appropriate amount needed?
if enteredList.find(mainString1) != 1:
print "You still need the following:"
print mainString1 - enteredList
you should stop writing program. :rolleyes:
Originally Posted by dbzakj
Please don't be rude. That's not constructive or helpful.
Originally Posted by satya prakash
dbzakj, what language is this? It looks like python, is that what you're using?
It is doubtful the subtraction operator is defined for strings (what it means to subtract a string is ambiguous, you could want it to do any number of things). You'll actually have to write code to make it do what you want, which is to replace that substring if it's present and perhaps some spaces as well.
Perhaps you should work with a data structure other than a string? Put your list into an actual list or array. That would make it easier to examine the items one at a time to see if they match the user input. Python has string.split() which would let you create a list from a string, given a space (' ') as the delimiter.
Then you can use list.remove() to remove the desired item, and list.count() to tell the user how many are in the list.
Yeah it's Python, for this specific code I have to deal with working with strings. I don't believe there is a substring to this.
str.replace() and str.count() will do the job, then; see above for the documentation link
Can you give an example of those? I don't think I know how they really work.
That's why I linked you to the webpage which explains how they work
enteredList = enteredList.replace('pearls', '')