String slicing in specific scenarios Python

I have a string I’d like to split to new strings which will contain only text (no commas, spaces, dots etc.). The length of each new string must be of variable n. The slicing must go through each possible combination. Meaning, for example, an input of func('banana pack', 3) will result in [‘ban’,’ana’,’nan’,’ana’,pac’,’ack’]. So far what I managed to achieve is:

def func(text, n):
    text = text.lower()
    text = text.translate(str.maketrans("", "", " .,"))
    remainder = len(text) % n
    split_text = [text[i:i + n] for i in range(0, len(text) - remainder, n)]
    if remainder > 0:
        split_text.append(text[-n:])
    return split_text

Answer

First I clean the input, by removing ',' and '.'. The input is then split at spaces to take only full words into account. For each word the sections are appended.

def func(text,n):
    text=text.replace('.','').replace(',','') #Cleanup
    words = text.split() #split words
    output = []
    for word in words:
        for i in range(len(word)-n+1):
            output.append(word[i:i+n])
    return output

You could unroll the loop one level if you just iterate over everything and discard results with unwanted symbols.