如何在python raw_input()函数中包含换行符? [英] How to include a newline in the python raw_input() function?

查看:246
本文介绍了如何在python raw_input()函数中包含换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 raw_input()将消息存储在变量中。因此,我无法按Enter键来输入回车符/换行符来开始新的段落。现在,如果我按Enter键,它将继续进行程序的下一部分。

I'm using raw_input() to storing a message inside a variable. So I can't press enter for a carriage return/new line to start a new paragraph. Right now if I press enter it will just proceed to the next portion of my program.

我已经尝试过类似的操作:

I already tried something like this:

>>> message = raw_input("Message: ")
Message: Hello Sir, \n It's great that..
>>> message
"Hello Sir, \\n It's great that.."
>>> 

它没有用,我也尝试用单引号和双引号将其括起来,但也没有

It didn't worked, and I also tried enclosing it in single and double quotes, which also didn't worked.

我知道还有其他方法可以执行此操作,例如使用 wxpython tkinter ,但我想严格控制它。

I understand that there are other ways of doing this, like using wxpython or tkinter, but I want to keep it strictly console. Is this possible?

推荐答案

可以使用 sys 模块吗?如果需要,这可以解决问题。只需按Ctrl-D即可结束。

Can you use the sys module? This will do the trick if you want. Just hit Ctrl-D to end it.

import sys
message = sys.stdin.readlines()

否则,这会回答您的问题: Python raw_input忽略换行符

Otherwise, this answers your question: Python raw_input ignore newline

这篇关于如何在python raw_input()函数中包含换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆