python中的換行符怎么用,python打印換行符_在Python編程中不使用換行符進行打印

 2023-11-19 阅读 33 评论 0

摘要:python打印換行符In different programming languages such as C, C++, Java, etc. by default, the printing statements do not end with a newline. 默認情況下,在不同的編程語言(例如C,C ++,Java等)中,打印語句

python打印換行符

Print Without Newline In Python

In different programming languages such as C, C++, Java, etc. by default, the printing statements do not end with a newline.

默認情況下,在不同的編程語言(例如C,C ++,Java等)中,打印語句不以換行符結尾。

python中的換行符怎么用。While in the case of Python, we see that the ‘print()’ function by default takes the cursor to the next line after printing the content in it. Let us look at an example where we try to print two different statements.

在使用Python的情況下,我們看到默認情況下, 'print()'函數在將內容打印后將光標移到下一行 。 讓我們看一個示例,其中我們嘗試打印兩個不同的語句。


print("Hello, this is Sneh.")
print("I love Python")

Output:

輸出:


Hello, this is Sneh.
I love Python

python編譯器。This could be useful while printing the content of each ‘print()’ statement in a newline. But sometimes the user may need to print stuff in the same line.

在換行符中打印每個“ print()”語句的內容時,這可能很有用。 但是有時用戶可能需要在同一行中打印內容。

This could be achieved by using any of the two methods mentioned below for Python 2.0+ or Python 3.0+ users.

這可以通過使用以下針對Python 2.0+Python 3.0+用戶的兩種方法中的任何一種來實現。

在Python 3.0+中無需換行即可打印 (Printing with no newlines in Python 3.0+)

python編程入門。In Python 3.0+ the ‘print()’ function comes with an additional optional argument ‘end’ which is actually nothing but the terminating string.

Python 3.0+中'print()'函數帶有一個附加的可選參數'end' ,實際上它只是終止字符串

Taking the same example as above, but this time using the ‘end’ argument let’s see if we can print both the statements in a single line.

以與上面相同的示例為例,但是這次使用'end'參數,讓我們看看是否可以在一行中打印兩個語句。


print("Hello, this is Sneh.", end="")
print("I love Python")

python打開文件?Output:

輸出:


Hello, this is Sneh.I love Python

So we can clearly see that just by giving any string into the print function as ‘end’ (as an argument) we can actually separate the print statements with them.

因此,我們可以清楚地看到,只要將任何字符串作為'end' (作為參數)輸入到print函數中,我們實際上就可以將它們與print語句分開。

用于不使用換行符打印列表 (For printing a list without Newline)

python爬蟲教程,We can similarly print the contents of a list or an Array without newlines as well. Let’s see how

我們同樣可以打印列表數組的內容而無需換行。 讓我們看看如何


list1=['God','is','Good']
for i in range(3):print(list1[i],end=" ")

Output:

輸出:


God is Good

在Python 2.0+中無需換行即可打印 (Printing with no newlines in Python 2.0+ )

python程序設計、For Python 2, we can solve the above-mentioned problem by any of the two methods. Firstly if we wan to separate the print statement contents with space( ” ” ) we can use the ‘,’ operator.

對于Python 2 ,我們可以通過兩種方法中的任何一種來解決上述問題。 首先,如果我們要用空格(””來分隔打印語句的內容,我們可以使用','運算符。

Whereas, for other separating string we can use the sys.stdout.write a function from the Sys module in Python 2.

而對于其他分隔字符串,我們可以使用sys.stdout.write來自Python 2中Sys模塊的函數。

python3,Again, for example, using ‘,’ operator,

同樣,例如,使用','運算符,


print("Hello, this is Sneh again!"), print("I love C++ too.")

Output:

輸出


Hello, this is Sneh again! I love C++ too.

python打印換行字符串,Using sys.stdout.write function in place of ‘print()’,

使用sys.stdout.write函數代替'print()',


import sys
sys.stdout.write("Hello, this is Sneh!")
sys.stdout.write("I love C++ too.")

Output:

輸出:


Hello, this is Sneh again!I love C++ too.

用于不使用換行符打印列表 (For printing a list without Newline)

使用','運算符 (Using ‘,’ operator)

python打印換行符。Again looking at an example,

再看一個例子,


list1=['Learn', 'Python', 'from', 'JournalDev']
for i in range(4):print(list1[i]),

Output:

輸出:


Learn Python from JournalDev

使用sys模塊功能 (Using the sys module function)

Look at this example carefully,

仔細看這個例子,


import sys
list1=['Learn', 'Python', 'form', 'JournalDev']
for i in range(4):sys.stdout.write(list1[i])sys.stdout.write(",")

Output:

輸出


Learn,Python,from,JournalDev

For learning more about the print function in Python, refer to this article from JounalDev https://www.journaldev.com/15182/python-print

要了解有關Python中的打印功能的更多信息,請參閱JounalDev的本文https://www.journaldev.com/15182/python-print

References:

參考文獻:

https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space

https://stackoverflow.com/questions/493386/how-to-print-without-newline-or-space

https://legacy.python.org/search/hypermail/python-1992/0115.html

https://legacy.python.org/search/hypermail/python-1992/0115.html

翻譯自: https://www.journaldev.com/34508/print-without-newline-in-python

python打印換行符

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/3/183286.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息