Suppose the goal is just to print all the items of a sequence, one on each line. The idea will be to print the first item (only for a nonempty sequence), then use recursion to deal with the rest of the sequence (where "the rest" of the sequence is done by slicing).

1
2
3
4
def PrintItems(S):
  if len(S) > 0:
    print S[0]
    PrintItems(S[1:])

Line 2 is where the clause only for a nonempty sequence takes effect. Line 4 combines a recursive call and the slice for "the rest".

Now suppose we only want to print items in S so long as they are strings. In other words, the very first item that is not a string should end the printing for all the rest of S. For this situation, two modifications of the code above are needed. First, there needs to be an if-test for the type of item; second, recursion should stop once it is found an item is not a string.

1
2
3
4
5
def PrintItems(S):
  if len(S) > 0:
    if type(S) == str:
       print S[0]
       PrintItems(S[1:])

In this second version, in order for recursion to happen, S must be nonempty and the first item of S has to be a string. Failure to meet these two conditions ends the function.