首页 > 解决方案 > 如何在python中打破长字符串?

问题描述

我有一个很长的字符串列表,我想知道是否有任何方法可以将每个字符串的元素用一定数量的单词(如底部的图像)分开。

List = [“End extreme poverty in all forms by 2030”,
“End hunger, achieve food security and improved nutrition and promote sustainable agriculture”,
“Ensure healthy lives and promote well-being for all at all ages”,
“Ensure inclusive and equitable quality education and promote lifelong learning opportunities for all”,
 “Promote sustained, inclusive and sustainable economic growth, full and productive employment and decent work for all”]

Print(list)

Out:
“End extreme poverty in all forms by 2030”,

“End hunger, achieve food security and improved 
nutrition and promote sustainable agriculture”,

“Ensure healthy lives and promote well-being for all at all ages”,

“Ensure inclusive and equitable quality education and promote 
lifelong learning opportunities for all”,

 “Promote sustained, inclusive and sustainable economic growth, 
full and productive employment and decent work for all”]

最终结果有点像图片

显示包裹的长文本的图表

标签: pythonstringlistmatplotlib

解决方案


您可以使用textwrap

import textwrap
print(textwrap.fill(List[1], 50))

End hunger, achieve food security and improved
nutrition and promote sustainable agriculture

推荐阅读