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

news/2024/7/3 1:09:23

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打印换行符


http://lihuaxi.xjx100.cn/news/237156.html

相关文章

【分布式共识三】拜占庭将军问题----书面协议

2019独角兽企业重金招聘Python工程师标准>>> 区块链兄弟社区&#xff0c;区块链技术专业问答先行者&#xff0c;中国区块链技术爱好者聚集地 作者&#xff1a;吴寿鹤 来源&#xff1a;区块链兄弟 原文链接&#xff1a;http://www.blockchainbrother.com/article/8 著…

xmpp 开源项目选择_如何选择和维护安全的开源项目

xmpp 开源项目选择评估开源项目安全性的一些技巧。 (A few tricks for assessing the security of an open source project.) There is a rather progressive sect of the software development world called the open source community. 在软件开发领域&#xff0c;有一个相当…

QT程序启动加载流程简介

1. QT应用程序启动加载流程简介1.1 QWS与QPA启动客户端程序区别1.1.1 QWS(Qt Window System)介绍QWS(Qt Windows System)是QT自行开发的窗口系统&#xff0c;体系结构类似X Windows的C/S结构。QWS Server在物理设备上显示&#xff0c;QWS Client实现界面&#xff0c;两者…

密码学是如何保护区块链的

链客&#xff0c;专为开发者而生&#xff0c;有问必答&#xff01; 此文章来自区块链技术社区&#xff0c;未经允许拒绝转载。 密码学是如何保护区块链的 摘要&#xff1a;密码学是应用数学函数以保证数据安全性的科学。 许多风靡的影视作品都在向人们暗示&#xff1a;只要有…

运用jieba库分词

代码&#xff1a; 统计出团队中文简介中词频 import jieba txtopen("C:\\Users\\Administrator\\Desktop\\介绍.txt","r",encodingutf-8).read() wordsjieba.lcut(txt) counts{} for word in words: if len(word)1: continue else: counts[word]counts.get…

react入门代码_如何在React中构建温度控制应用程序-包括提示和入门代码

react入门代码我们正在建立的 (What were building) In this beginner React project, were going to learn how to use state hooks, handle events, apply CSS based on state, and more! Check it out: 在这个初学者的React项目中&#xff0c;我们将学习如何使用状态挂钩&am…

西默科技宣布完成1.56亿元B轮融资,兰博尔集团投资

9月3日消息&#xff0c;据相关媒体报道&#xff0c;西默科技宣布已于去年获得1.56 亿元B轮融资&#xff0c;由兰博尔集团独家投资。 西默科技CEO 黄基明表示&#xff0c;本轮融资将用于APP 平台和产品研发、品牌推广、终端门店建设等方面。 西默科技成立于2009年&#xff0c;…

在php中将数组作为树遍历

问题&#xff1a;在php中将数组作为树遍历 我有一个描述层次结构的数据库表。这是结构 id | pid | uid 1 5 22 2 33 2 44 2 65 3 7在树形结构中,它看起来是这样的。这只是一个例子,它们可能是更多的节点。 2 / | \3 4 6/ 7 因此…