python如何多行输入

原创
admin 19小时前 阅读数 2 #Python

Python中的多行输入可以通过使用三重引号来实现,三重引号可以用来定义多行字符串,它们可以是单引号或双引号。

以下是一个使用三重单引号的Python代码示例,它定义了一个包含多行文本的字符串:

text = '''This is a multi-line string.
It can be used to store multiple lines of text.
Each line can be a different length, and they can all be treated as one string.'''
print(text)

输出:

This is a multi-line string.
It can be used to store multiple lines of text.
Each line can be a different length, and they can all be treated as one string.

在这个示例中,三重单引号之间的所有内容都被视为一个字符串,包括所有的换行符和空格,你可以根据需要添加任意数量的行和文本。

如果你想要使用双引号来定义多行字符串,可以使用以下代码:

text = """This is a multi-line string.
It can be used to store multiple lines of text.
Each line can be a different length, and they can all be treated as one string."""
print(text)

输出:

This is a multi-line string.
It can be used to store multiple lines of text.
Each line can be a different length, and they can all be treated as one string.

在这个示例中,三重双引号之间的所有内容都被视为一个字符串,包括所有的换行符和空格,你可以根据需要添加任意数量的行和文本。

热门