一尘不染

如何在Python中读取多行原始输入?

python

我想创建一个Python程序,该程序需要多行用户输入。例如:

This is a multilined input.
It has multiple sentences.
Each sentence is on a newline.

如何接收多行原始输入?


阅读 805

收藏
2020-02-17

共1个答案

一尘不染

sentinel = '' # ends when this string is seen
for line in iter(raw_input, sentinel):
    pass # do things here

要将每一行作为字符串获取,你可以执行以下操作:

'\n'.join(iter(raw_input, sentinel))

Python 3:

'\n'.join(iter(input, sentinel))
2020-02-17