python:使用for循环将字符串分解为子字符串

python:使用for循环将字符串分解为子字符串,python,Python,我有这样一个字符串: 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

我有这样一个字符串:

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个字符或更少)

看看模块。

我认为最清晰的方法是

for i in range(0, len(row), 55):
    print test[i:i+55]
其中
范围
的第三个参数是
步长
值,即范围元素之间的差值

编辑:只要重读你的问题,我就知道我不确定你是想让它沿着单词边界断裂,还是在每行的第55个字符处进行硬断裂


如果您确实想让它沿着单词边界断裂,您应该像unutbu和silenghost一样检查模块。

您所说的“切割空格”是什么意思?
textwrap
可以在单词边界内断裂吗?呃,等等,我想这就是OP想要的。。我只是重读了这个问题。
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