python字符串为什么不能修改_为什么在Python(\n)中更改字符串中的行不起作用?...

在大多数情况下,它可以完成任务,但有时(我很难精确地说,它依赖于什么)它会陷入一个无限循环中,因为它不切分文本字符串。在def insertNewlines(text, lineLength):

"""

Given text and a desired line length, wrap the text as a typewriter would.

Insert a newline character ("\n") after each word that reaches or exceeds

the desired line length.

text: a string containing the text to wrap.

line_length: the number of characters to include on a line before wrapping

the next word.

returns: a string, with newline characters inserted appropriately.

"""

def spacja(text, lineLength):

return text.find(' ', lineLength-1)

if len(text) <= lineLength:

return text

else:

x = spacja(text, lineLength)

return text[:x] + '\n' + insertNewlines(text[x+1:], lineLength)

适用于所有我试过的案子除了

^{pr2}$

以及insertNewlines('mubqhci sixfkt pmcwskvn ikvoawtl rxmtc ehsruk efha cigs itaujqe pfylcoqw iremcty cmlvqjz uzswa ezuw vcsodjk fsjbyz nkhzaoct', 38)

我不知道为什么。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值