Python 中的文字換行
我們將介紹如何在 python 中包裝文字。Python 有一個內建模組 textwrap
,可以幫助我們實現此功能。
本教程將通過使用 textwrap
模組的不同示例。
Python 中的文字換行
許多情況需要我們對字串進行包裝以提高可見性、可讀性或其他原因。我們可能需要為特定的螢幕尺寸換行文字,這樣文字就不會溢位螢幕的大小。
如果我們根據每行的字元數來包裝文字,它可能會破壞單詞並使其有時無法閱讀並降低使用者體驗。textwrap
模組提供了不同的方法來包裝長文字。
在 Python 中使用 wrap()
方法
一種常用的方法是 wrap(content, width=length)
。內容是我們需要換行的文字,寬度表示每行的字元數。
預設字元數為 70
。包裝文字後,我們將使用 join()
方法將行連線成單個字串。
示例程式碼:
# python
import textwrap
content = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
wrappedText = textwrap.wrap(content)
print("\n".join(wrappedText))
輸出:
我們的文字很容易包裝,而不會破壞上面示例中的單詞。
現在讓我們來看另一個示例,在該示例中我們將討論如果我們只想顯示字串或文字段落中的特定行數該怎麼做。
我們將列印相同的文字段落,但我們將限制每行的字元數以增加行數,並將僅顯示 10 行並放置一個佔位符來代替剩餘的行,如下所示。
示例程式碼:
# python
import textwrap
content = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
# Without Limiting Number of Lines
wrappedText = textwrap.wrap(content, width=40)
print("Number of Lines : {}\n".format(len(wrappedText)))
print("\n".join(wrappedText))
# With Limiting Number of Lines
wrappedText = textwrap.wrap(
content, width=40, max_lines=10, placeholder="more content....."
)
print("Number of Lines : {}\n".format(len(wrappedText)))
print("\n".join(wrappedText))
不限制行數的輸出:
用於限制行數的輸出:
我們不僅可以限制每行的字元數,還可以限制要顯示的行數,我們還可以放置一個佔位符來代替剩餘的行。
在 Python 中使用 fill()
方法
wrap()
方法返回一個行列表,而不是返回帶有換行文字的整個段落。
但是為了這個目的,Python 提供了另一種稱為 fill()
的方法,它的功能與 wrap()
方法相同,但返回一個組合每一行的單個字串,而不是返回一個行列表。
示例程式碼:
# python
import textwrap
content = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum."
wrappedText = textwrap.fill(content)
print(wrappedText)
print("\n")
wrappedText = textwrap.fill(content, width=40)
print("\n")
print(wrappedText)
wrappedText = textwrap.fill(
content, width=40, max_lines=12, placeholder=" [..More Content]"
)
print("\n")
print(wrappedText)
輸出:
本教程討論了 textwrap
模組中的 wrap()
方法及其引數。我們還討論了 fill()
方法及其引數。
Rana is a computer science graduate passionate about helping people to build and diagnose scalable web application problems and problems developers face across the full-stack.
LinkedIn