ฟังก์ชั่น wrap(text, width=70, **kwargs)
โค้ด: เลือกทั้งหมด
import textwrap
text = 'This function wraps the input paragraph such that each line in the paragraph is at most width characters long. ' \
'The wrap method returns a list of output lines. The returned list is empty if the wrapped output has no content'
wrapper = textwrap.TextWrapper(width=50)
word_list = wrapper.wrap(text=text)
for element in word_list:
print(element)
โค้ด: เลือกทั้งหมด
This function wraps the input paragraph such that
each line in the paragraph is at most width
characters long. The wrap method returns a list of
output lines. The returned list is empty if the
wrapped output has no content
โค้ด: เลือกทั้งหมด
import textwrap
value = """This function returns the answer as STRING and not LIST."""
wrapper = textwrap.TextWrapper(width=50)
string = wrapper.fill(text=value)
print (string)
โค้ด: เลือกทั้งหมด
This function returns the answer as STRING and not
LIST.
โค้ด: เลือกทั้งหมด
import textwrap
wrapper = textwrap.TextWrapper(width=50)
s = '''\
hello
world
'''
print(repr(s))
text = textwrap.dedent(s)
print(repr(text))
โค้ด: เลือกทั้งหมด
'\\ \n hello \n world \n '
'\\ \n hello \n world \n'
อ้างอิง
https://docs.python.org/2/library/textwrap.html
https://www.geeksforgeeks.org/textwrap-text-wrapping-filling-python/
https://pymotw.com/3/textwrap/