Answers for "python get all substrings of length k"

0

python get all substrings of length k

string = input()
k = int(input())
answers = []
for start in range(len(string)):
    temp = string[start: start + k]
    if len(temp) == k:
        answers.append(temp)

print(answers)

answers = [string[start: start + k] for start in range(len(string))
           if len(string[start: start + k]) == k]
# in single line but less efficient
print(answers)
Posted by: Guest on April-08-2021

Code answers related to "python get all substrings of length k"

Python Answers by Framework

Browse Popular Code Answers by Language