【问题标题】:Accept newline character in python在python中接受换行符
【发布时间】:2013-06-12 05:53:25
【问题描述】:

我想在 python 中接受换行输入。

我在努力

s = input('''Enter the paragraph :''')

但是当我输入段落时.. 在第一个 ENTER 上它停止进一步输入行。

那么如何在 Python 中接受换行符作为输入

然后如何打印屏幕上的段落。?

【问题讨论】:

标签: python


【解决方案1】:

编辑:此答案假定您使用的是 Python 3。如果您使用的是 Python 2.x,请使用 Anthon 提供的那个。

做这样的事情:

text = ''
while True: # change this condition.
    text += input('''Enter the paragraph :''')+'\n' #UPDATED. Appended a \n character.

例如,您想以一个额外的换行符结束输入序列,那么代码将是:

text = ''
while True:
    dummy = input('''Enter the paragraph :''')+'\n'
    if dummy=='\n':
        break
    text += dummy

【讨论】:

  • 我应该在while..中施加什么条件?
  • 这取决于你想如何结束输入。我将更新答案。
  • 有一个布尔值,当您输入某个终止字符串时,该值变为 false。
  • 我假设 OP 使用 Python 3。我更新了答案以显示我的假设。
【解决方案2】:

来自Python 3: receive user input including newline characters

我做了以下事情:

import sys


text = sys.stdin.read()
print(text)

这就是我的任务。

【讨论】:

    猜你喜欢
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 2017-07-30
    • 2012-09-27
    • 2015-06-25
    • 1970-01-01
    • 2017-06-17
    • 1970-01-01
    相关资源
    最近更新 更多