465,503 views
41 votes
41 votes
Write a docstring for this function

Write a docstring for this function-example-1
User Chris Haas
by
2.8k points

1 Answer

20 votes
20 votes

Answer:

def word_wrap(message, length=79):

"""

Wrap a message to a specified line length.

Args:

message (str): The message to be wrapped.

length (int): The maximum line length. Default value is 79.

Returns:

str: The wrapped message.

"""

lines = message.split('\\')

output = []

for line in lines:

words = line.split(" ")

if len(words) > 0:

output_line = words[0]

for word in words[1:]:

if len(output_line + f' {word} ') > length:

output.append(output_line)

output_line = f"{word}"

else:

output_line = f"{output_line} {word}"

output.append(output_line)

return '\\'.join(output)

User Recf
by
3.6k points