python打印换行符_在Python编程中不使用换行符进行打印

在Python中,默认的'print()'函数会在打印内容后换行。要避免此行为,Python 3用户可以使用'end'参数指定结束字符串,Python 2用户则可以使用','运算符或'sys.stdout.write()'函数。本文通过示例展示了如何在一行内打印多个语句和列表内容。" 132876994,19671262,MIMO-OFDM系统误码率Matlab仿真分析,"['无线通信', 'MIMO技术', 'OFDM技术', 'Matlab仿真', '通信系统']
摘要由CSDN通过智能技术生成

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等)中,打印语句不以换行符结尾。

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

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+)

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")

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)

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+ )

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模块的函数。

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.

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)

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值