Kelime Sarma
Uzun metni belirtilen sütun genişliğinde sarar, kelime veya karakter kesmesi seçenekleriyle.
20200
Sıkça Sorulan Sorular
Kod Uygulaması
def word_wrap(text: str, width: int = 80, hard: bool = False) -> str:
"""Wrap text to the specified width.
Args:
text: Input text (may contain existing newlines).
width: Maximum line length in characters.
hard: If True, break mid-word at exactly 'width' chars.
"""
import textwrap
if hard:
# Hard wrap: split each existing line at 'width' chars
lines = []
for line in text.splitlines(keepends=True):
while len(line) > width:
lines.append(line[:width] + "\n")
line = line[width:]
lines.append(line)
return "".join(lines)
else:
# Soft wrap: only break at whitespace
paragraphs = text.split("\n\n")
wrapped = [textwrap.fill(p, width=width) for p in paragraphs]
return "\n\n".join(wrapped)
text = ("The quick brown fox jumps over the lazy dog. "
"Pack my box with five dozen liquor jugs. "
"How vexingly quick daft zebras jump!")
print(word_wrap(text, width=40))Comments & Feedback
Comments are powered by Giscus. Sign in with GitHub to leave a comment.