博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python打印换行符_在Python编程中不使用换行符进行打印
阅读量:2543 次
发布时间:2019-05-11

本文共 3570 字,大约阅读时间需要 11 分钟。

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 syssys.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 syslist1=['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

要了解有关Python中的打印功能的更多信息,请参阅JounalDev的本文

References:

参考文献:

翻译自:

python打印换行符

转载地址:http://ztqzd.baihongyu.com/

你可能感兴趣的文章
机器学习降维之主成分分析
查看>>
CTP2交易所成交回报
查看>>
WebSocket & websockets
查看>>
openssl 升级
查看>>
ASP.NET MVC:通过 FileResult 向 浏览器 发送文件
查看>>
CVE-2010-2883Adobe Reader和Acrobat CoolType.dll栈缓冲区溢出漏洞分析
查看>>
使用正确的姿势跨域
查看>>
AccountManager教程
查看>>
Android学习笔记(十一)——从意图返回结果
查看>>
算法导论笔记(四)算法分析常用符号
查看>>
ultraedit激活
查看>>
总结(6)--- python基础知识点小结(细全)
查看>>
亿级曝光品牌视频的幕后设定
查看>>
ARPA
查看>>
JSP开发模式
查看>>
我的Android进阶之旅------>Android嵌入图像InsetDrawable的使用方法
查看>>
Detours信息泄漏漏洞
查看>>
win32使用拖放文件
查看>>
Android 动态显示和隐藏软键盘
查看>>
raid5什么意思?怎样做raid5?raid5 几块硬盘?
查看>>