Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
2 func for 6 lines total solution in Creative category for Text Formatting by CDG.Axel
from textwrap import wrap
def format_line(line, width, style):
# first divmod argument is total spaces needed, second - intervals between words
d, q = divmod(width - len(line) + len(lst := line.split()) - 1, max(1, len(lst) - 1))
return f"{line:{'<^>'['lcr'.find(style)]}{width}}".rstrip() if style in 'lcr' \
else ''.join(' ' * (i > 0) * (d + (i <= q)) + w for i, w in enumerate(lst))
def text_formatting(text: str, width: int, style: str) -> str:
lst = wrap(text, width=width)
return '\n'.join(line if style == 'j' and i+1 == len(lst) else
format_line(line, width, style) for i, line in enumerate(lst))
"""
# no wordwrap.wrap alternative with comments
res = line = ''
for w in text.split():
if len(line) + len(w) + 1 > width:
line, res = '', res + format_line(line, width, style) + '\n'
line += ' ' * bool(line) + w
return res + (line if style == 'j' else format_line(line, width, style))
"""
Oct. 13, 2021
Comments: