How to get all substrings of length K in Python

1 Answer

0 votes
def all_substrings_of_length_k(s, k):
    return [s[i:i+k] for i in range(len(s) - k + 1)]

result = all_substrings_of_length_k("abcdef", 3)

print(result)


'''
run:

['abc', 'bcd', 'cde', 'def']

'''

 



answered Mar 8 by avibootz

Related questions

...