Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
map locals()[style] solution in Clear category for Text Formatting by flpo
from textwrap import wrap
def text_formatting(text: str, width: int, style: str) -> str:
def l(line): return line
def c(line): return line.center(width).rstrip()
def r(line): return line.rjust(width)
def j(line):
if text.endswith(line): return line
words = line.split()
s, e = divmod(width - len(line), len(words) -1)
justified = (w + ' ' * (s + (1 * i < e)) for i, w in enumerate(words))
return ' '.join(justified).rstrip()
return '\n'.join(map(locals()[style], wrap(text, width)))
May 12, 2019
Comments: