Second for loop
Your second loop will only iterate once, because
string is only 1 character long:
string = l[i]
I think you meant to slice here.
You can use the
+= operator to increment an integer:
x = 0
for i in range(3):
x += 1
For the most part, you can use
str.startswith to check if the suffix starts with the prefix (other than the case where prefix is
prefix, suffix = 'a', 'abc'
This is a bit more idiomatic than slicing from position
j to compare with a prefix
Creating your prefix and suffix
When generating your prefix and suffix, slice once, and you can keep your code down to one loop:
for i in range(len(l)):
# Generate your prefix and suffix here
prefix, suffix = l[:i], l[i:]
# rest of code
Instead of a counter, use an if statement
For actually tabulating your results, you can use an if statement, paired with the
str.startswith logic to cut down on your second loop.
Furthermore, it appears from the example that if the prefix is an empty string, you append the length of the suffix. This can go in the
if str.startswith bit because
'anystr'.startswith('') will return
results = 
# Start your loop
for i in range(len(mystring)):
# Create your prefix and suffix based on slice point
prefix, suffix = mystring[:i], mystring[i:]
# check str.startswith here
# You can use a ternary if block in the append to cover the
# empty string case
results.append(len(prefix) if prefix else len(suffix))
The ternary operator will result in either the left side if the predicate is truthy, otherwise the right side
x = 'a' if True else 'b'
x = 'a' if False else 'b'
x = 'a' if '' else 'b'
Empty strings are falsey.
Method names should be
# do things
There should be a space between a name, the equals sign, and a value:
# instead of this
# do this
some_value = 3
The exception to this is with keyword arguments in a function
# No extra spaces here
Be careful about name shadowing
string is the name of a module, so watch out for cases where you might acidentally name a variable something that you may import (or users of your code might import). Not a huge deal in this case, but certainly something to watch out for. Note that this accounts mostly for common modules and functions, both built-in and 3rd party like