如何在 python 中使用用户输入带有换行符的文本?

How can I user-input text with line breaks in python?

我也在尝试使用歌词作为用户输入,并且在每节经文之后换行。 Python 将新行识别为“ENTER”,并将只处理第一节。如何在带有换行符的单个输入中输入空洞歌词?

user_input = input('Input song lyrics: ')

输入: 我们对爱并不陌生 你知道规则,我也知道 完全的承诺是我所想的 你不会从任何其他人那里得到这个

print (user_input)

输出: 我们对爱并不陌生

当写入输入而不是按回车键时写入 \n。这是 python 理解为换行符的标志。

示例:

"We're no strangers to love \nYou know the rules and so do I \nA full commitment's what I'm thinking of \nYou wouldn't get this from any other guy"

不用担心 \n 和下一个字符之间没有 space,python 会将 \n 解释为跳转到新行然后从下一个单词开始的叹息。您甚至可以在 \n 之前删除 space,因为这会消除每行末尾或您的情况下的 space。

基于此,您无法轻松获得多行输入,解决方案如下:

print('Input song lyrics: ')

x =  input() 
inp = []
while x != '':  
    inp.append(x) 
    x = input()
print(inp)

缺点是用户应该输入空行来结束它