Home > Backend Development > Python Tutorial > How to use print to output multiple lines in python

How to use print to output multiple lines in python

下次还敢
Release: 2024-05-05 20:27:49
Original
1013 people have browsed it

There are two common ways to output multi-line text in Python: use the newline character \n to break the text into lines. Use three quotes to create a multi-line string and new lines will be added automatically.

How to use print to output multiple lines in python

How to use print to output multiple lines in Python

In Python, there are many ways to output multiple lines. OK. Here are the two most common methods:

1. Use newline character

Use newline character \n to split the text into new lines . For example:

<code class="python">print("行 1\n行 2\n行 3")</code>
Copy after login

Output:

<code>行 1
行 2
行 3</code>
Copy after login

2. Using a multi-line string

Use three single quotes or three double quotes to create multiple line string. A new line is automatically added to the end of each line in the string. For example:

<code class="python">print("""
行 1
行 2
行 3
""")</code>
Copy after login

The output is the same as using newlines.

Other Notes:

  • Make sure you use the correct quotes for your strings. Using unmatched quotes will result in an error.
  • For longer strings, using multiline strings is often more concise and more readable.
  • You can also use the end parameter to specify a newline character. For example, end=" " will add a space to the end of each line instead of a newline character.

The above is the detailed content of How to use print to output multiple lines in python. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template