[英]Why does this string mess with my python print statement
I came across this error when I was trying to print out some user defined strings. 当我尝试打印一些用户定义的字符串时遇到了这个错误。 When I print out this string I get a weird interaction with the print statement and I am not sure what causes this.
当我打印出该字符串时,我与print语句发生了奇怪的交互,并且我不确定是什么原因导致的。 Can anyone help me on the right track?
谁能在正确的道路上帮助我? I'd be happy to post more information if it's needed!
如果需要,我很乐意发布更多信息!
In the following example the input "my_message" was used 在以下示例中,使用了输入“ my_message”
Relevant code snippet: 相关代码段:
print("APPENDING [[" + "test1" + "]]")
print("APPENDING [[" + message + "]]")
print(message)
Console output for snippet: 代码段的控制台输出:
APPENDING [[test1]]
]]PENDING [[my_message
my_message
It's almost certainly a '\\r
' (carriage return) hiding out in the string. 几乎肯定是隐藏在字符串中的
'\\r
'(回车符)。 Try stripping the message: 尝试剥离消息:
print("APPENDING [[" + "test1" + "]]")
print("APPENDING [[" + message.strip() + "]]")
The carriage return will cause output to jump back to the beginning of the line, which is why you see the ]]
overwriting part of the line. 回车将使输出跳回到该行的开头,这就是为什么您看到该行的
]]
覆盖部分的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.