简体   繁体   English

为什么这个字符串弄乱了我的python打印语句

[英]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.

相关问题 为什么我的python祝福打印语句未在同一位置打印? - Why does my python blessings print statement not print in the same spot? 为什么第一个打印语句的字符串会打印在 python 中? - Why does the first print statement's string gets printed in python? 为什么我的 Python 打印语句不断重复? - Why does my Python print statement keep getting repeated? 为什么我的输入 function 打印我的 else 语句而不是我定义的字符串? - Why does my my input function print my else statement instead of the string i define? 为什么第8行中的打印语句无法运行? - Why does my print statement in line 8 not run? 为什么 Python 在这里打印 else 语句? - Why does Python print the else statement here? 为什么我的基本打印语句有效,但我的 f-string 版本会产生语法错误? - Why does my basic print statement work, but my f-string version produces a syntax error? 为什么即使 python 中的条件为真,我的 while 语句的主体也不打印 - Why does does the body of my while statement not print even though the condition is true in python python中的导入语句混乱 - import statement mess in python 为什么我的 main 方法底部的 print 语句不打印任何内容? - Why does the print statement at the bottom of my main method not print anything?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM