删除for循环中的空格

Bub*_*ter 3 python whitespace loops for-loop

我在python中创建了一个简单的脚本,它使用chr和ord将字母在ASCII表中向上移动了5个空格.见下文:

word = "python"

print 'Shifted 5 letters are: '
for letters in word:    
    print chr(ord(letters)+5),
Run Code Online (Sandbox Code Playgroud)

输出是:

Shifted 5 letters is: 
u ~ y m t s
Run Code Online (Sandbox Code Playgroud)

输出很棒,但是如何停止for循环在每个字母之间放置空格?

qap*_*hla 9

如果您不需要使用for循环,只需执行以下操作:

print ''.join([chr(ord(letter) + 5) for letter in word])
Run Code Online (Sandbox Code Playgroud)

而不是整个循环.

  • [This](http://docs.python.org/2/library/stdtypes.html#str.join)应该为您提供有关`''.join`和[this]的信息(http:// docs .python.org/2/tutorial/datastructures.html#list-comprehensions)其余部分. (5认同)
  • 谢谢,这有效,但它是如何工作的?我需要阅读的内容是什么?是字符串操作吗?谢谢 (2认同)