如何在字符串中移动空白?
我需要将一个弦的空格移动到右侧的一个位置。 这是我的代码:
for i in range(0,len(resultaat)):
if resultaat[i] == " ":
string = resultaat[:i] + resultaat[i+1] + " " + resultaat[i+2:]
例如: 如果结果=
"TH EZE NO FPYTHON."
我的输出需要:
'THE ZEN OF PYTHON.'
,但是我得到的输出是:
"TH EZE NO F PYTHON."
我认为这发生了,因为循环解开了它移动上一个空间的动作。 我不知道如何解决这个问题。 有人可以帮我吗?
谢谢!
I need to move a whitespace in a string one position to the right.
This is my code:
for i in range(0,len(resultaat)):
if resultaat[i] == " ":
string = resultaat[:i] + resultaat[i+1] + " " + resultaat[i+2:]
E.g.:
If resultaat =
"TH EZE NO FPYTHON."
Than my output needs to be:
'THE ZEN OF PYTHON.'
, but the output that I get is:
"TH EZE NO F PYTHON."
I think this happened because the loop undid the action where it moved the previous space.
I don't know how to fix this problem.
Can someone help me with this?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
每次通过循环,您都将获得原始
Resultaat
字符串的切片,而无需对以前的迭代进行更改。首先,您应该将
RESTERAAT
将其复制到String
首先,然后将其用作每个切片的来源,以便您累积所有更改。Each time through the loop you're getting slices of the original
resultaat
string, without the changes you've made for previous iterations.You should copy
resultaat
tostring
first, then use that as the source of each slice so you accumulate all the changes.您可以做这样的事情:
You could do something like this:
假设所述的输入值实际上比实际需要的空间多:
输出:
Assuming the stated input value actually has one more space than is actually needed then:
Output: