我有一个这样的字符串:
row='saint george 1739 1799 violin concerti g 029 039 050 symphonie concertante for two violins g 024 bertrand cervera in 024 039 christophe guiot in 024 029 and thibault vieux violin soloists orchestre les archets de paris'
我有这个循环:
for n in range (1,int(len(row)/55)+1):
print row[(n-1)*55:n*55]
运作良好!!
然而,它正在削减空间:saint george 1739 1799 violin concerti g 029 039 050 sy
mphonie concertante for two violins g 024 bertrand cerv
era in 024 039 christophe guiot in 024 029 and thibault
我不希望它削减空格(但我仍然希望每行55个字符或更少)
答案 0 :(得分:4)
import textwrap
row='saint george 1739 1799 violin concerti g 029 039 050 symphonie concertante for two violins g 024 bertrand cervera in 024 039 christophe guiot in 024 029 and thibault vieux violin soloists orchestre les archets de paris'
print(textwrap.fill(row,width=55))
# saint george 1739 1799 violin concerti g 029 039 050
# symphonie concertante for two violins g 024 bertrand
# cervera in 024 039 christophe guiot in 024 029 and
# thibault vieux violin soloists orchestre les archets de
# paris
答案 1 :(得分:3)
查看textwrap
模块。
答案 2 :(得分:1)