python - 如何在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”]
最终结果有点像图片
解决方案
您可以使用textwrap
:
import textwrap
print(textwrap.fill(List[1], 50))
End hunger, achieve food security and improved
nutrition and promote sustainable agriculture
推荐阅读
- javascript - React-Native - 如何替换 react-native 核心组件的功能
- php - 如何将“amp-state”提交给我在“action-xhr”上调用的网络服务?
- neo4j - 仅返回两个节点之间的特定关系(来自一种类型的多个)
- php - 如何在php中显示多个行的值?
- optaplanner - Optaplanner 限制 valueRangeProvider 基于另一个实体
- python - 无法从 python 模块 md5hash 导入
- java - 空指针异常 - 依赖注入 - Google Guice
- javascript - 如何使用 CurrentUserId 在 Firebase 中进行 CRUD 操作?
- visual-studio-2013 - 自定义 Window Vista 示例现有包装凭证提供程序
- elasticsearch - 在 filebeat 源路径中使用变量