find a substring in a string python
<p>This might be a beginner question but I can't seem to find the answer anywhere</p>

<p>How do I search and return a substring from another string in which the charachers are in a different order ?</p>

<p>When I check with the code below it seems to give the right answer but I'm trying to print it rather getting True or False and also, when I submit it, it says "Incorrect. Your submission did not return the correct result for the input ('UdaciousUdacitee', 'Udacity'). Your submission passed 3 out of 4 test cases:" .... I'm confused..and I've been wrapping my brain around for 3 hours or so.</p>

<p>Thank you</p>

<pre><code>Test case 1: False 
Test case 2: True 
Test case 3: True 
Test case 4: True
</code></pre>

<p>More precisely:</p>

<pre><code>def fix_machine(debris, product):
  if debris.find(product):
   return product
  else:
   print("Give me something that's not useless next time.")


print "Test case 1: ", fix_machine('UdaciousUdacitee', 'Udacity') == "Give me something that's not useless next time."
print "Test case 2: ", fix_machine('buy me dat Unicorn', 'Udacity') == 'Udacity'
print "Test case 3: ", fix_machine('AEIOU and sometimes y... c', 'Udacity') == 'Udacity'
print "Test case 4: ", fix_machine('wsx0-=mttrhix', 't-shirt') == 't-shirt'
</code></pre>

<p>You used <code>str.find()</code> wrong.</p>

<pre><code>"It determines if string str occurs in string, or in a substring of string if starting index beg and ending index end are given."
</code></pre>

<p>It will consider the order, which is not what you want. Change your <code>fix_machine</code> to:</p>

<pre><code>def fix_machine(debris, product):
  charNumInDebris = dict()
  charNumInProduct = dict()

  for c in debris:
    if c in charNumInDebris:
      charNumInDebris[c] += 1
    else:
      charNumInDebris[c] = 1

  for c in product:
    if c in charNumInProduct:
      charNumInProduct[c] += 1
    else:
      charNumInProduct[c] = 1

  for c in charNumInProduct:
    if not (c in charNumInDebris and charNumInDebris[c] &gt;= charNumInProduct[c]):
      return "Give me something that's not useless next time."

  return product
</code></pre>

python string substring