If you really want to use regex for your task then I suggest you use this -
import re
print(re.findall(r'(?=(\w\w\w))', 'Jonathan'))
You can increase or decrease the number of \w
's, depending on how many length-n
sub-strings you want.
Output -
['Jon', 'ona', 'nat', 'ath', 'tha', 'han']
Another example -
print(re.findall(r'(?=(\w\w\w\w))', 'Jonathan'))
Output -
['Jona', 'onat', 'nath', 'atha', 'than']
Hope this helps!
Following your recent comment, here's something that might work -
Example 1 -
import re
s = "amam"
m = re.compile(".m.")
h = m.findall(s)
print(h)
Output -
['ama']
Example 2 -
import re
s = "Jonathan"
m = re.compile(".o.")
h = m.findall(s)
print(h)
Output -
['Jon']
Example 3 -
import re
s = "Jonathanona"
m = re.compile(".o.")
h = m.findall(s)
print(h)
Output -
['Jon', 'non']
Hope this helps!