为什么日食+ pydev print() 输出看起来很奇怪,有两个字符串?

发布于 2024-08-26 09:46:57 字数 498 浏览 9 评论 0原文

干草,我刚刚做了以下操作:

a = input("give a word: ")
b = input("give another word: ")

c = a + " " + b

print("result is", c)

并得到如下输出:

give a word: name
give another word: word
result is name
word

我的问题是为什么 pydev 或 eclipse 控制台上的输出分为两行?我期望输出如下:

give a word: name
give another word: word
result is name word

这种情况如何以及为什么发生在我身上?在 cmd 上看起来不错???!

我还发现 python 用“\r”保存字符串,我认为这是在 pydev 控制台上造成这个问题的原因,是吗?

hay all, I just did the following:

a = input("give a word: ")
b = input("give another word: ")

c = a + " " + b

print("result is", c)

and get the output as follows:

give a word: name
give another word: word
result is name
word

my question is why the output on pydev or eclipse console in two lines? i expected to output as follows:

give a word: name
give another word: word
result is name word

how and why this happens to me? on cmd its looking fine??!!

also i found that python saves the strings with "\r", i think that is the one making this problem on pydev console, is it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

鹿港巷口少年归 2024-09-02 09:46:57

在我看来,Eclipse + PyDev 也在字符串中存储换行符。根据操作系统的不同,换行符有几种变体:\n、\r、\r\n。

无论如何,我认为以下内容应该可以解决您的问题:

a = raw_input("give a word: ").strip()
b = raw_input("give another word: ").strip()
c = a + " " + b

我已经在 Windows7 上的 Eclipse Galileo 的 PyDev 上测试了这段代码,它可以工作。

希望这有帮助

It seems to me that Eclipse + PyDev is storing the newline character in the string as well. There are a few variants of the newline character depending on the operating system: \n, \r, \r\n.

In any case, I think the following should fix your problem:

a = raw_input("give a word: ").strip()
b = raw_input("give another word: ").strip()
c = a + " " + b

I have tested this code on PyDev for Eclipse Galileo on Windows7 and it works.

Hope this helps

红墙和绿瓦 2024-09-02 09:46:57

这很奇怪。

您是否在单词后收到额外的换行符? (您可以通过发出另一个打印命令来检查)。

Eclipse 在控制台输入上总是很奇怪。如果它以某种方式在字符串中保留 CR 或 LF (或两者),我不会感到惊讶,这样当您打印它们中的每一个时,您就会得到换行符。但是你应该在单词之后再换行。

That's very strange.

Are you getting an extra newline after word? (you can check by issuing another print command).

Eclipse is always weird on console input. I would not be surprised if somehow it keeps a CR or a LF (or both) in the string, so that when you print each of them, you would get a line break. But then you should get another line break after word.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文