# Python – Extract K length substrings

There are many problems in which we require to get all K length substrings of a string. This particular utility is very popular in competitive programming and having shorthands to solve this problems can always be handy. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using list comprehension + string slicing
The combination of list comprehension and string slicing can be used to perform this particular task. This is just brute force method to perform this task.

 `# Python3 code to demonstrate working of ` `# Extract K length substrings ` `# Using list comprehension + string slicing ` ` `  `# initializing string  ` `test_str ``=` `"Geeks"` ` `  `# printing original string  ` `print``(``"The original string is : "` `+` `str``(test_str)) ` ` `  `# initializing K  ` `K ``=` `3` ` `  `# Extract K length substrings ` `# Using list comprehension + string slicing ` `res ``=` `[test_str[i: j] ``for` `i ``in` `range``(``len``(test_str)) ``for` `j ``in` `range``(i ``+` `1``, ``len``(test_str) ``+` `1``) ``if` `len``(test_str[i:j]) ``=``=` `K] ` ` `  `# printing result  ` `print``(``"All K length substrings of string are : "` `+` `str``(res)) `

Output :

```The original string is : Geeks
All K length substrings of string are : ['Gee', 'eek', 'eks']
```

Method #2 : Using `itertools.combinations()`
This particular task can also be performed using the inbuilt function of combinations, which helps to get all K length the possible combinations i.e the substrings from a string.

 `# Python3 code to demonstrate working of ` `# Extract K length substrings ` `# Using itertools.combinations() ` `from` `itertools ``import` `combinations ` ` `  `# initializing string  ` `test_str ``=` `"Geeks"` ` `  `# printing original string  ` `print``(``"The original string is : "` `+` `str``(test_str)) ` ` `  `# initializing K  ` `K ``=` `3` ` `  `# Extract K length substrings ` `# Using itertools.combinations() ` `res ``=` `[test_str[x:y] ``for` `x, y ``in` `combinations(``range``(``len``(test_str) ``+` `1``), r ``=` `2``) ``if` `len``(test_str[x:y]) ``=``=` `K ] ` ` `  `# printing result  ` `print``(``"All K length substrings of string are : "` `+` `str``(res)) `

Output :

```The original string is : Geeks
All K length substrings of string are : ['Gee', 'eek', 'eks']
```

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.