gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 13:05:32 24 4
gpt4 key购买 nike

我有这样一个字符串:

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

最佳答案

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

关于python:使用for循环将字符串分解为子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3886465/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com