0

Apologies for the poor title, I don't know how else to describe my situation.

I wrote a small pattern matching function.

def substrings(input_str):
    '''Generate all leading substrings of an input string'''
    for i in range(len(input_str)):
        return input_str[:i]

It should return a series of slices of a string. If the input was ABCD, it should output ABCD, ABC, AB and A.

When I tested this function in the python console (shown below), it behaves correctly and outputs all the expected strings.

for i in range(len(input_str)):
    print(input_str[:i])

But when used in the body of my program its returning nothing at all. For example;

test1 = substrings('ABCD')
print(test1)

Outputs blank lines and I'm struggling to figure out why.

4
  • 3
    I don't think this code would have worked in the console either. It's always returning on the first iteration of the loop. Commented Apr 1, 2019 at 15:49
  • What it will return though is "ABCD"[:0],which is an empty string. I'm not sure what you're intent is there. Commented Apr 1, 2019 at 15:51
  • Okay, now Im really confused. Im definitely getting the right output in console, though it is also outputting blank strings before and after the sub strings. Commented Apr 1, 2019 at 15:55
  • Again, I can't see this code as posted ever working. It will only ever return a single thing, and as setup, that single thing will be an empty string. Note that def substrings(s): return [s[i:] for i in range (len(s))] almost does what you want, although it's a little backwards. Commented Apr 1, 2019 at 15:56

4 Answers 4

1

In the console, your loop first prints out a blank string when i==0. Then it continues to loop and print out each of the characters in the string.

In the function, you are returning up to the 0th element in the array, which is the same blank string the console printed on the first time through the loop.

To see better what is happening in the console, you might print the index too:

for i in range(len(input_str)):
  print('{0} {1}'.format(i, input_str[:i]))
Sign up to request clarification or add additional context in comments.

Comments

1

That's because the first thing your functions returns is empty string ''. So you are exiting loop after first iteration for i = 0 and your variable is empty string because of the fact that:

>>> s = 'ABCD'
>>> s[:0]
''

Comments

1

You are returning in a loop. So the return is the last statement that would execute in a function. That is when a return statement is reached, the control leaves the function. So in the very first iteration i=0, the control returns '' and exits the function irrespective of for-loop. In console the output is obtained because I'm console each line is interpreted one-by-one unlike the program being compiled at once. So console shows the output. Hope this answer helps you

Comments

0

The issue is occurring because you are trying to return information in a loop. When the return statement is called, it exits the function. What you should do is use the yield keyword. Try doing this

def substrings(input_str):
    for i in range(len(input_str)):
        yield input_str[:i]

# Then iterate through the function like so

function = substrings()
for i in function:
    print(i)

Your code should be working fine now!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.