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

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

  1. java final修饰符_浅谈JAVA中的final修饰符

    final修饰符是JAVA中比较简单的一个修饰符,很多人通过书本就可以熟练掌握,但我学的时候,虽然感觉会用了,但是并不是十分理解这个修饰符,所以我找了相关资料,把我的个人理解分享给大家~~ final ...

  2. python列表换行写入_如何使用Python3中的换行符将列表写入文件

    我正在尝试使用Python 3将数组(列表?)写入文本文件 . 目前我有: def save_to_file(*text): with open('/path/to/filename.txt', mo ...

  3. python怎么去掉换行符_在Python中,如何去除行末的换行符?

    原标题:在Python中,如何去除行末的换行符? 在python中读取文件时,如何去除行末的换行符?以及在Windows与Linux中的区别? 一.去除换行符 以使用readline进行读取为例: i ...

  4. python之禅 中文_《Python之禅》中对于Python编程过程中的一些建议

    <Python之禅>中对于Python编程过程中的一些建议 来源:中文源码网    浏览: 次    日期:2018年9月2日 [下载文档:  <Python之禅>中对于Pyt ...

  5. python 打印数组变量_使用Python将数组的元素导出到变量中(unpacking)

    下面就为大家分享一篇使用Python将数组的元素导出到变量中(unpacking),具有很好的参考价值,希望对大家有所帮助.一起过来看看吧 最近工作中遇到一个问题,需要利用Python将数组(list ...

  6. python语言程序设计编程题_《python语言程序设计》_第一章编程题

    题目1.1 :显示"welcome to python " 答案:print('welcome to python') 题目1.2:显示"welcome to pytho ...

  7. python文件输出中文_【python】中文的输出,打印,文件编码问题解决方法

    直接在python中输入中文的字符串会报编译错误SyntaxError: Non-ASCII character,因为python文件默认编码方式是ASCII.如果想要打印中文字符,有两种方式: 1. ...

  8. python加入中小学课程_【python即将进入中学课堂,编程从小抓起,竟然在这几点上应验了】- 环球网校...

    [摘要]我们都知道,不论是我们使用的app,还是各种各样的游戏,小程序,都离不开编程软件的贡献,其中python占据了半壁江山,随着大家对python的重视,python即将进入中学课堂,这究竟是好是 ...

  9. python 打印皮卡丘_用python打印你的宠物小精灵吧

    我们来通过一个有趣的例子开始编写我们的第一个python代码. 本文涉及的python基础语法为:print输出函数,赋值,字符串 print() print()是python的一个内置函数,用于打印 ...

最新文章

  1. Hadoop之Storm命令
  2. 他们拿走腾讯广告百万奖金,我算是知道票圈广告为啥那么多赞了
  3. 检测到smtp服务器版本信息,邮件服务器DBMail检测功能
  4. C++游戏入门书籍推荐
  5. qwt+qt5.4.1+win7 环境搭建(完美版)
  6. 浅析下关于js的 逗号运算符 和 改变this指向 的一道题(mv to git)
  7. 【bzoj1758】[Wc2010]重建计划
  8. linux下个性配置命令提示符
  9. gitlab 数据同步
  10. C#中,如何随意拖动PictureBox?
  11. Ubuntu 16.04安装Docker
  12. 大学生计算机考证时间表
  13. CentOS下Nginx+fastcgi+python3搭建web.py服务环境
  14. 斯芬克怎么样 谁说我没有担心
  15. 传输设备基础知识〖 一篇就看懂〗
  16. UVa-202 Repeating Decimals
  17. 无线路由如何快速设置WDS扩展网络
  18. 《Python基础教程(第3版)》笔记:第8章异常
  19. scala特质 对比java的接口 使用方法
  20. BeagleBone Black Industrial系统更新设置一贴通

热门文章

  1. android 之反编译
  2. [转载] python的短逻辑
  3. CF1110G Tree-Tac-Toe 博弈论、构造
  4. Python字符的转义
  5. 页面校验请求MmEwMD(转载)
  6. NPM私有服务器搭建方法——sinopia
  7. 编写高质量javascript代码的基本要点
  8. UI框架-JQuery Smart / 淘宝JS库 KISSY UI
  9. 侯捷推荐的C++书单
  10. 使用EDITPLUS编写C#控制台应用程序