python打印换行符_Python换行符以及如何在不使用换行符的情况下进行Python打印

python打印换行符

Welcome! The new line character in Python is used to mark the end of a line and the beginning of a new line. Knowing how to use it is essential if you want to print output to the console and work with files.

欢迎! Python中的新行字符用于标记行的结尾和新行的开始。 如果您想将输出打印到控制台并使用文件,则知道如何使用它至关重要。

In this article, you will learn:

在本文中,您将学习:

  • How to identify the new line character in Python.

    如何在Python中识别换行符。
  • How the new line character can be used in strings and print statements.

    如何在字符串和打印语句中使用换行符。
  • How you can write print statements that don't add a new line character to the end of the string.

    如何编写不会在字符串末尾添加换行符的打印语句。

Let's begin! 🔅

让我们开始! 🔅

换行符 (The New Line Character )

The new line character in Python is:

Python中的换行符是:

It is made of two characters:

它由两个字符组成:

  • A backslash.

    反斜杠。
  • The letter n.

    字母n

If you see this character in a string, that means that the current line ends at that point and a new line starts right after it:

如果您在字符串中看到此字符,则表示当前行在该点结束,而新行在其后立即开始:

You can also use this character in f-strings:

您也可以在f字符串中使用此字符:

>>> print(f"Hello\nWorld!")

Print打印语句中的换行符 (🔹 The New Line Character in Print Statements)

By default, print statements add a new line character "behind the scenes" at the end of the string.

默认情况下,print语句在字符串的末尾在“幕后”添加新的换行符。

Like this:

像这样:

This occurs because, according to the Python Documentation:

发生这种情况是因为,根据Python文档

The default value of the end parameter of the built-in print function is \n, so a new line character is appended to the string.

内置print功能的end参数的默认值为\n ,因此在该字符串后附加了一个换行符。

💡 Tip: Append means "add to the end".

💡提示:追加表示“添加到末尾”。

This is the function definition:

这是函数定义:

Notice that the value of end is \n, so this will be added to the end of the string.

请注意, end值为\n ,因此它将被添加到字符串的末尾。

If you only use one print statement, you won't notice this because only one line will be printed:

如果仅使用一个打印语句,您将不会注意到这一点,因为将仅打印一行:

But if you use several print statements one after the other in a Python script:

但是,如果您在Python脚本中一个接一个地使用多个打印语句:

The output will be printed in separate lines because \n has been added "behind the scenes" to the end of each line:

输出将被打印在单独的行中,因为\n已被“幕后”添加到每行的末尾:

🔸如何在没有换行的情况下进行打印 (🔸 How to Print Without a New Line)

We can change this default behavior by customizing the value of the end parameter of the print function.

我们可以通过自定义print功能的end参数的值来更改此默认行为。

If we use the default value in this example:

如果在此示例中使用默认值:

We see the output printed in two lines:

我们看到输出显示为两行:

But if we customize the value of end and set it to " "

但是,如果我们自定义end的值并将其设置为" "

A space will be added to the end of the string instead of the new line character \n, so the output of the two print statements will be displayed in the same line:

字符串的末尾将添加一个空格,而不是换行符\n ,因此两个打印语句的输出将显示在同一行:

You can use this to print a sequence of values in one line, like in this example:

您可以使用它在一行中打印一系列值,例如以下示例:

The output is:

输出为:

💡 Tip: We add a conditional statement to make sure that the comma will not be added to the last number of the sequence.

提示:我们添加一个条件语句,以确保不会将逗号添加到序列的最后一个数字中。

Similarly, we can use this to print the values of an iterable in the same line:

同样,我们可以使用它在同一行中打印iterable的值:

The output is:

输出为:

Files文件中的换行符 (🔹 The New Line Character in Files)

The new line character \n is also found in files, but it is "hidden". When you see a new line in a text file, a new line character \n has been inserted.

在文件中也可以找到换行符\n ,但是它是“隐藏的”。 当您在文本文件中看到新行时,已插入新行字符\n

You can check this by reading the file with <file>.readlines(), like this:

您可以通过使用<file>.readlines()读取文件来进行检查,如下所示:

with open("names.txt", "r") as f:
    print(f.readlines())

The output is:

输出为:

As you can see, the first three lines of the text file end with a new line \n character that works "behind the scenes."

如您所见,文本文件的前三行以\n字符结尾,在“幕后”工作。

💡 Tip: Notice that only the last line of the file doesn't end with a new line character.

提示:请注意,只有文件的最后一行不以换行符结尾。

Summary总结 (🔸 In Summary)

  • The new line character in Python is \n. It is used to indicate the end of a line of text.

    Python中的换行符为\n 。 它用于指示一行文本的结尾。

  • You can print strings without adding a new line with end = <character>, which <character> is the character that will be used to separate the lines.

    您可以打印字符串而无需添加带有end = <character>的新行,其中<character>是将用于分隔行的字符。

I really hope that you liked my article and found it helpful. Now you can work with the new line character in Python.

我真的希望您喜欢我的文章并发现它对您有所帮助。 现在,您可以在Python中使用换行符。

Check out my online courses. Follow me on Twitter. 👍

查看我的在线课程 。 在Twitter上关注我。 👍

翻译自: https://www.freecodecamp.org/news/python-new-line-and-how-to-python-print-without-a-newline/

python打印换行符

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值