python如何输入多行,Python多行输入方法指南

原创
ithorizon 7个月前 (09-28) 阅读数 42 #Python

Python中的多行输入方法

在Python中,可以使用三种方法来实现多行输入:

1、使用三重引号('''""")来定义多行字符串。

text = '''This is a multi-line string.
It contains several lines of text.
We can use it to store multiple lines of data.'''
print(text)

2、使用括号(())来定义多行表达式。

result = (1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 +
            11 + 12 + 13 + 14 + 15 + 16 + 17 + 18 +
            19 + 20)
print(result)

3、使用反斜杠(\)来连接多行。

text = "This is a multi-line string.\n" \
       "It contains several lines of text.\n" \
       "We can use it to store multiple lines of data."
print(text)

三种方法都可以实现Python中的多行输入,具体使用哪种方法取决于你的需求。



热门