简体   繁体   English

如何摆脱最后的空白?

[英]how to get rid of the whitespace at the end?

i'm just a beginner to python and i have been asked to solve a problem where your program has to decode a code in which it takes every 3rd letter and puts them together with a gap in between each letter, sorry that was a bad explanation but have a look at my code:我只是 python 的初学者,我被要求解决一个问题,你的程序必须解码一个代码,在这个代码中,它需要每 3 个字母并将它们放在一起,每个字母之间有一个间隙,抱歉,这是一个错误的解释但看看我的代码:

    m=input("Message? ")
    for c in m[::3]:
      print(c,end=' ')

the problem is there's a whitespace after the last letter and i dont know how to get rid of it, i've tried.rstrip and that doesnt work.问题是在最后一个字母之后有一个空格,我不知道如何去掉它,我已经试过了。rstrip 但这不起作用。 i've also tried to [::-1]after the end=' ' and that doesn't work.我也试过 [::-1]after the end=' ' 但这不起作用。

You could use str.join on a list comprehension rather than a loop:您可以在列表推导式而不是循环中使用str.join

" ".join(m[::3])

This would put a space between each letter, excluding the trails.这将在每个字母之间放置一个空格,不包括路径。

I'm not sure what the problem is exactly but I'm assuming you mean to say that on your last print you do not want the end=' ' whitespace.我不确定问题到底是什么,但我假设你的意思是说在你最后一次打印时你不想要 end=' ' 空白。 To produce the string you want you can join the slice on whitespace as lltt said, minus the redundant list comprehension.要生成您想要的字符串,您可以像lltt所说的那样在空白处加入切片,减去冗余列表理解。

m = "123456789"
print(" ".join(m[::3]))

Output: Output:

"1 4 7"

If you really want to maintain your for loop print functionality you need a way to determine if you are in the last loop.如果你真的想维护你的 for 循环打印功能,你需要一种方法来确定你是否在最后一个循环中。 This is a general problem which has various solutions such as a look-ahead function. But you can simply break up the for loop into two parts.这是一个有多种解决方案的一般问题,例如先行 function。但是您可以简单地将 for 循环分成两部分。

m="123456789"
for c in m[::3][:-1]:
    print(c,end=' ')
print(m[::3][-1], end='')

Output: Output:

1 4 7

Of course you do not need the end='' in the last print;当然你不需要最后打印的end='' print(m[::3][-1]) is fine. print(m[::3][-1])很好。 It is just there for readability.它只是为了可读性。

If you mean to say that the decoded message cannot have a whitespace as the last character then you can use.rstrip().如果你的意思是说解码后的消息不能有空格作为最后一个字符,那么你可以使用 .rstrip()。 For example when the last, third character of the input, is a whitespace.例如,当输入的最后一个、第三个字符是空格时。

m = "input with trailing whitespace "
print(" ".join(m[::3]))

Output: Output:

"i u w h r l g h e a  "

Here you can chain the rstrip() method.在这里您可以链接 rstrip() 方法。

m = "input with trailing whitespace "
print(" ".join(m[::3]).rstrip())

Output: Output:

"i u w h r l g h e a"

When working with strings, you should avoid hand-crafted solutions that are inefficient and often incorrect.使用字符串时,您应该避免效率低下且通常不正确的手工解决方案。 Connect the letters with spaces using the string method .join() :使用字符串方法.join()将字母与空格连接起来:

m = "Hello, world!"
print(" ".join(m[::3]))
#'H l w l'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM