def insertNewline(text, length):
'''
This function can only be applied to normal text.
'''
if len(text) <= length:
print(text)
return
elif text[length - 1] == ' ':
print(text[:length - 1])
return insertNewline(text[(length):], length)
elif text[length] == ' ':
print(text[:length])
return insertNewline(text[(length + 1):], length)
else:
i = 0
while text[length + i] != ' ' and text[length + i] != ',' and text[length + i] != '.':
i += 1
if text[length + i] == ' ':
print(text[:(length + i)])
return insertNewline(text[(length + i + 1):], length)
else:
print(text[:(length + i + 1)])
return insertNewline(text[(length + i + 2):], length)
This code works correctly with normal text, by which I mean simple English sentences with only ',' and '.' as punctuations, and after each punctuation there is only one space, just as normal essay writing. If you want to apply this code to more complete text, I think you can update this code by yourself.
I use "recursion" as you required in your question. I think this code should be helpful for you.
I'm not an expert in python, and still in the learning process. Hope you can get what you want from my answer.