Python中使用Join方法为什么会莫名输出空格?
正在做 pythonchallenge,Q2,我自己用的是字典的方法
words ="g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj. "
secret = {“a”:“c”,“b”:“d”,“c”:“e”,“d”:“f”,“e”:“g”,“f”:"h ",“g”:“i”,"h ":“j”,“i”:“k”,“j”:“l”,“k”:“m”,“l”:“n”,“m”:“o”,“n”:“p”,“o”:“q”,“p”:“r”,“q”:“s”,“r”:“t”,“s”:“u”,“t”:“v”,“u”:“w”,“v”:“x”,“w”:“y”,“x”:“z”,“y”:“a”,“z”:“b”}
answer=[]
i=0
while i < len(words):
j = words[i]
if j in secret.keys():
answer.append(secret[j])
else:
answer.append(j)
i = i +1
print(answer)
print("".join(answer))
测试发现 最后的 answer 这个 list 是没有问题的, 内容是
['i', ' ', 'h ', 'o', 'p', 'e', ' ', 'y', 'o', 'u', ' ', 'd', 'i', 'd', 'n', 't', ' ', 't', 'r', 'a', 'n', 's', 'l', 'a', 't', 'e', ' ', 'i', 't', ' ', 'b', 'y', ' ', 'h ', 'a', 'n', 'd', '.', ' ', 't', 'h ', 'a', 't', 's', ' ', 'w', 'h ', 'a', 't', ' ', 'c', 'o', 'm', 'p', 'u', 't', 'e', 'r', 's', ' ', 'a', 'r', 'e', ' ', 'f', 'o', 'r', '.', ' ', 'd', 'o', 'i', 'n', 'g', ' ', 'i', 't', ' ', 'i', 'n', ' ', 'b', 'y', ' ', 'h ', 'a', 'n', 'd', ' ', 'i', 's', ' ', 'i', 'n', 'e', 'f', 'f', 'i', 'c', 'i', 'e', 'n', 't', ' ', 'a', 'n', 'd', ' ', 't', 'h ', 'a', 't', "'", 's', ' ', 'w', 'h ', 'y', ' ', 't', 'h ', 'i', 's', ' ', 't', 'e', 'x', 't', ' ', 'i', 's', ' ', 's', 'o', ' ', 'l', 'o', 'n', 'g', '.', ' ', 'u', 's', 'i', 'n', 'g', ' ', 's', 't', 'r', 'i', 'n', 'g', '.', 'm', 'a', 'k', 'e', 't', 'r', 'a', 'n', 's', '(', ')', ' ', 'i', 's', ' ', 'r', 'e', 'c', 'o', 'm', 'm', 'e', 'n', 'd', 'e', 'd', '.', ' ', 'n', 'o', 'w', ' ', 'a', 'p', 'p', 'l', 'y', ' ', 'o', 'n', ' ', 't', 'h ', 'e', ' ', 'u', 'r', 'l', '.', ' ']
但是最后使用 join 将 list 转变成 str 时,结果就变成了
i h ope you didnt translate it by h and. th ats wh at computers are for. doing it in by h and is inefficient and th at's wh y th is text is so long. using string.maketrans() is recommended. now apply on th e url.
注意其中莫名奇妙的多了几个空格,正常应该是
i hope you didnt translate it by hand. thats what computers are for. doing it in by hand is inefficient and that's why this text is so long. using string.maketrans() is recommended. now apply on the url.
请教下大家这莫名奇妙的空格是怎么被输出的?
Python中使用Join方法为什么会莫名输出空格?
你遇到的情况,很可能是因为 join() 方法连接的是字符串列表,而列表中的字符串本身就包含了空格(比如 "Hello " 或 " World"),或者你用来分隔的字符串就是空格(比如 " ")。
join() 方法本身不会“莫名”添加空格。它的作用很简单:用指定的字符串把列表中的所有元素连接成一个新字符串。问题通常出在被连接的元素上,而不是 join() 本身。
看几个例子就明白了:
情况1:列表元素自身带空格
words = ["Hello ", " World"]
result = "".join(words)
print(repr(result)) # 输出:'Hello World'
# 注意 'Hello' 后面和 'World' 前面各有一个空格,它们来自列表元素。
情况2:分隔符是空格
words = ["Hello", "World"]
result = " ".join(words) # 分隔符是一个空格
print(repr(result)) # 输出:'Hello World'
# 这是正常用法,空格是作为分隔符明确指定的。
情况3:最常见的“坑”:打印时用逗号分隔
words = ["Hello", "World"]
print("".join(words), "!") # 注意这里的逗号
# 输出:HelloWorld !
# 这是因为 print() 函数中多个参数会用空格分隔输出,和 join() 无关。
正确做法应该是:
print("".join(words) + "!")
# 或者
result = "".join(words) + "!"
print(result)
如何排查?
- 检查你的列表:打印列表本身,看看每个元素是不是你想象的样子。
print(repr(my_list))的repr()函数能显示字符串里的空白字符。 - 检查你的分隔符:确认
join()前面的字符串是什么,是空字符串""还是空格" "或者其他。 - 检查 print 语句:确保没有在
print()中误用逗号导致添加了额外的空格。
总结建议:用 repr() 打印变量来检查隐藏字符。
answer 的 list 里面不也是有空格吗
这也太明显了,左右还有两个引号
…果然是低级错误。用 emeditor 批量替换成字典的时候多打了空格。。
Strip 一下就好
遇到问题还是再多想一下吧,这么硬敲的时候不想偷下懒么?这密码对是有规律的啊
扔个 one-liner(逃):
print(*[secret.get(s, s) for s in words], sep=’’)
习惯性所有输入处理都带上 trim 就看不到这问题了
如果遇到 words.upper()这样的字符你的程序就失效了啊,字典再扩大一倍?


