python get all substrings of length k

Solutions on MaxInterview for python get all substrings of length k by the best coders in the world

showing results for - "python get all substrings of length k"
Clara
30 Jan 2018
1string = input()
2k = int(input())
3answers = []
4for start in range(len(string)):
5    temp = string[start: start + k]
6    if len(temp) == k:
7        answers.append(temp)
8
9print(answers)
10
11answers = [string[start: start + k] for start in range(len(string))
12           if len(string[start: start + k]) == k]
13# in single line but less efficient
14print(answers)