title: Create Your Own Find String Function
body: <p>For a school project I have to create a function called find_str that essentially does the same thing as the .find string method, but we cannot use any string methods in our definition. </p>
  
  <p>The project description reads: "Function find_str has two parameters (both strings). It returns the lowest index where the second parameter is found within the first parameter (it returns -1 if the second parameter is not found within the first parameter)."</p>
  
  <p>I have spent a lot of time working on this project and have yet to come to a solution. This is the current definition that I have come up with: </p>
  
  <pre><code>def find_str (string, substring):
      index = 0
      length = len (substring)
      for ch in string:
          if ch == substring [0]:
              subindex1 = 0
              subindex2 = index
              for i in range (length):
                  if ch == substring [i]:
                      subindex1 +=1
                      if subindex1 == length:
                          return index
                      ch = string [(subindex2)+1]
                      subindex2 +=1
          index += 1
      return "-1"
  </code></pre>
  
  <p>This sample of code only works in some instances, but not all.</p>
  
  <p>For example:</p>
  
  <pre><code>print (find_str ("hello", "llo"))
  </code></pre>
  
  <p>returns:</p>
  
  <pre><code>2
  </code></pre>
  
  <p>as it should. </p>
  
  <p>But</p>
  
  <pre><code>print (find_str ("hello", "el"))
  </code></pre>
  
  <p>returns: </p>
  
  <pre><code>   ch = string [(subindex2)+1]
  IndexError: string index out of range
  </code></pre>
  
  <p>I feel like I am overthinking this and there must be is an easier way to do it. Any input or help would be great! Thanks. </p>