I need help with writing a loop that can slice through a string, taking away the first and last character. I understand that the slice [0:-1] can target those two positions, but I need a way for it to iterate through the whole string. Here is a little snippet into what the output would ideally look like:
Input (min for length is 3) :
string = 'ABCDEFGHI'
Output:
['ABCDEFGHI', 'BCDEFGH', 'CDEFG']
I would be grateful for any guidance/advice!
Try this:
>>> [string[i:len(string)-i] for i in range(3)] ['ABCDEFGHI', 'BCDEFGH', 'CDEFG']
2.1m questions
2.1m answers
60 comments
56.9k users