python code to add new lines so that each text line has a limit of n1

Code:


def add_new_lines(text, n1):
    words = text.split(' ')
    lines = []
    current_line = []
    current_length = 0

    for word in words:
        if current_length + len(word) + len(current_line) > n1:
            lines.append(' '.join(current_line))
            current_line = [word]
            current_length = len(word)
        else:
            current_line.append(word)
            current_length += len(word)

    lines.append(' '.join(current_line))
    return '\n'.join(lines)