สมมติว่าเรามีสตริง s และความกว้าง w ต้องจัดรูปแบบข้อความนี้เป็นย่อหน้าที่มีความกว้าง w สามารถทำได้ง่ายด้วย fill() จากไลบรารี textwrap
ตัวอย่าง s = "The quick brown fox jumps over the lazy dog" w = 9 ผลลัพธ์:
The quick brown fox jumps over the lazy dog
ขั้นตอนการแก้ปัญหา
- นำเข้า textwrap
- เรียก textwrap.fill(s, w)
ตัวอย่างโค้ด
import textwrap
def solve(s, w):
return textwrap.fill(s, w)
s = "The quick brown fox jumps over the lazy dog"
w = 9
print(solve(s, w))
อินพุต
"The quick brown fox jumps over the lazy dog", 9
เอาต์พุต
The quick brown fox jumps over the lazy dog