2025-09-10:仓库迁移
This commit is contained in:
20
2.字符串和文本/16.以固定列数重新格式化文本.py
Normal file
20
2.字符串和文本/16.以固定列数重新格式化文本.py
Normal file
@@ -0,0 +1,20 @@
|
||||
import textwrap
|
||||
import os
|
||||
|
||||
if __name__ == '__main__':
|
||||
words = ' '.join(["look", "into", "my", "eyes", "look", "into", "my", "eyes",
|
||||
"the", "eyes", "the", "eyes", "the", "eyes", "not", "around", "the",
|
||||
"eyes", "don't", "look", "around", "the", "eyes", "look", "into",
|
||||
"my", "eyes", "you're", "under"])
|
||||
print(words)
|
||||
# 可以看到这样打印出来的字符串巨长无比,如果想要控制行宽度,就用textwrap模块
|
||||
print(textwrap.fill(words, 70))
|
||||
print(textwrap.fill(words, 40))
|
||||
# 第二个参数width可以控制显示宽度,如果是控制台输出,你还能去找os.get_terminal_size()
|
||||
# 注意,由于未知原因这在pycharm中不好使(2024.8.27)
|
||||
t_width = os.get_terminal_size().columns
|
||||
print(t_width)
|
||||
print(textwrap.fill(words, t_width))
|
||||
|
||||
# 用的应该不多,在需要显示长字符串的时候去查textwrap库就行
|
||||
|
Reference in New Issue
Block a user