一、让print()函数不换行

  在Python中,print()函数默认是换行的。但是,在很多情况下,我们需要不换行的输出(比如在算法竞赛中)。那么,在Python中如何做到这一点呢?

  其实很简单。只要指定print()函数的end参数为空就可以了。(默认是’\n’)

  例如:

1 print('hello world', end='')
2 print('!!!')

  输出为:

二、print()函数浅析

  当然,print()函数不止有end这个参数,还有其它几个参数。下面我们来看一看这些参数对输出分别起到什么作用。

  先来看一下print()函数的原型:

print(*objectssep='  'end='\n'file=sys.stdoutflush=False)

  以下摘自官方文档:

Print objects to the text stream file, separated by sep and followed by endsependfile and flush, if present, must be given as keyword arguments.

All non-keyword arguments are converted to strings like str() does and written to the stream, separated by sep and followed by end. Both sep and end must be strings; they can also be None, which means to use the default values. If no objects are given, print() will just write end.

The file argument must be an object with a write(string) method; if it is not present or None, sys.stdout will be used. Since printed arguments are converted to text strings, print() cannot be used with binary mode file objects. For these, use file.write(...) instead.

Whether output is buffered is usually determined by file, but if the flush keyword argument is true, the stream is forcibly flushed.

Changed in version 3.3: Added the flush keyword argument.

  也就是说,print()将objects转换成strings输出到流中,用sep分隔,以end结束。

  下面通过几个例子,来具体的看一看print()函数各参数的作用。

  第一个参数objects就不多说了,要是不知道干啥的可以考虑从头学起。

  第二个参数sep,表示objects参数连接时使用的字符,默认是空格。

1 print('hello', 'world', sep='*')

  输出为:

  第三个参数end,表示输出完后的结束符,默认是换行。例子前面有了。

  第四个参数file,表示输出到哪里,默认是sys.stdout。必须是file-like对象,即有write方法,不可以用二进制模式。

1 f = open('print.txt', 'w')
2 print('hello', 'world', file=f)

  程序运行结束后,打开文件可以看到:

  第五个参数flush,表示是否立即输出到file所指定的对象中。当为True时,立即输出,当为False时,则取决于file对象(一般是不立即输出)。

  上面的例子,如果加个暂停,可以发现,数据没有被立即写入,只有在f.close()后才被写入。如果没有写f.close(),那就在程序运行结束以后写入。

1 f = open('print.txt', 'w')
2 print('hello', 'world', file=f)
3 s = input('f.close()? (Y/N)')
4 if s == 'Y':
5     f.close()

  如果flush为True时,则会被立即写入。

1 f = open('print.txt', 'w')
2 print('hello', 'world', file=f, flush=True)
3 s = input('f.close()? (Y/N)')
4 if s == 'Y':
5     f.close()

  以上就是对Python中print()函数的浅析,鉴于本人的水平有限,有不妥之处,还请指出。

 1 #!/usr/bin/env python3
 2 # -*- coding: utf-8 -*-
 3
 4 # 不换行
 5 print('hello world', end='')
 6 print('!!!')
 7
 8 f = open('print.txt', 'w')
 9 f1 = open('print1.txt', 'w')
10
11 print('hello', 'world')
12 print('hello', 'world', sep='*')
13 print('hello', 'world', file=f)
14 print('hello', 'world', file=f1, flush=True)
15
16 s = input('f.close()? (Y/N)')
17 if s == 'Y':
18     f.close()
19
20 # 如果输入为N,此处暂停观察print.txt中的内容是否为空
21 s = input()

转载于:https://www.cnblogs.com/Bil369/p/9534399.html

Python中print()函数不换行的方法相关推荐

  1. python2print函数不换行_Python中print()函数不换行的方法以及分隔符替换

    在Python中,print()函数默认是换行的.但是,在很多情况下,我们需要不换行的输出(比如在算法竞赛中).那么,在Python中如何做到这一点呢? 其实很简单.只要指定print()函数的end ...

  2. python中print函数定义及使用方法

    print() 方法用于打印输出,最常用的一个函数. 函数定义: print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False) 参数 ...

  3. Python3中print函数的换行

    Python3中print函数的换行 最近看了看Python的应用,从入门级的九九乘法表开始,结果发现Python3.x和Python2.x真的是有太大的不同之处,就比如这里的换行处理,怕忘记先记下来 ...

  4. python中print()换行的问题

    title: python中print()换行的问题 date: 2022-04-17 14:36:12 tags: - python使用 - python学习 categories: python ...

  5. Python中Print()函数的用法___实例详解(二)(全,例多)

    Python中Print()函数的用法___实例详解(二)(全,例多) 目录 十一.Print()小例子 十二.Print()中文输入显示乱码问题 十三.Print()写入文件 十四.print()在 ...

  6. python3打印如何换行_浅谈Python3中print函数的换行

    Python3中print函数的换行 最近看了看Python的应用,从入门级的九九乘法表开始,结果发现Python3.x和Python2.x真的是有太大的不同之处,就比如这里的换行处理,怕忘记先记下来 ...

  7. python中bool函数用法_在python中bool函数的取值方法

    bool是Boolean的缩写,只有真(True)和假(False)两种取值 bool函数只有一个参数,并根据这个参数的值返回真或者假. 1.当对数字使用bool函数时,0返回假(False),任何其 ...

  8. 第5天-[21天学Python]-Python中自定义函数及调用的方法

    本章内容主要包括: 声明函数 调用自定义函数 变量作用域 各种类型的函数参数应用 使用lambda建立匿名函数 Python其他常用内建函数 1.使用函数 1.1 声明函数 在python中,函数必须 ...

  9. python中round函数的精度保留方法---四舍六入五成双

    当我们利用python进行数据计算时,通常会对浮点数保留相应的位数,这时候就会用到round函数,相信各位朋友在进行使用时会遇到各种问题,关于round函数保留精度.保留方法的问题,本文会进行详细的解 ...

最新文章

  1. 怎样Selenium IDE选择combo box中值
  2. 用电脑更新手机ios系统_手机系统频繁提示更新,到底要不要更新?
  3. jenkins gitlab php,jenkins(8): 实战jenkins+gitlab持续集成发布php项目(代码不需要编译)...
  4. IT巨头组队年末将推“Gen-Z”内存新架构,英特尔和思科缺席是闹哪样?
  5. gpu处理信号_GPU中的并行运算,加速你的Matlab程序
  6. 【转】在C#中使用SQLite
  7. 计算机技术在机械设计中的应用,计算机技术在机械设计制造和自动化中的应用(原稿)...
  8. 08 内存分配和程序内存布局
  9. 如何使用 vSphere Certificate Manager 替换 SSL 证书 (2097936)
  10. 开发软件快捷键(持续更新中)
  11. HttpServletRequest 需要导入xx包?
  12. 使用Nemiver调试器来调试 C/C++ 程序
  13. 转场动画UINavigationControllerDelegate
  14. mGBA-0.9.2 免费开源的gba模拟器
  15. 小甲鱼老师目前所有视频教程下载地址
  16. Halcon实战之MLP颜色匹配(一)
  17. 华硕 x570 Ryzen 9 5900X电脑 Hackintosh 黑苹果efi引导文件
  18. LightOJ1197 Help Hanzo(欧拉筛+区间素数)
  19. laravel 常用文档
  20. 10.2.2.7 -DHCP 和 DNS 服务

热门文章

  1. [Luogu 1730]最小密度路径
  2. HDU 4857 逃生 (反向拓扑排序 容器实现)
  3. Gym 100733G No Negations
  4. MongoDB副本集配置系列六:定位MongoDB慢的原因
  5. 002..NET MVC实现自己的TempBag
  6. durpal是否支持php7,drupal7 的安装方法
  7. 古代的碎银子是怎么来的?
  8. 发生心梗后,家属做些什么才能保证患者获救,降低死亡?
  9. 大学生在校期间可以考哪些证书?
  10. 宛如造句,小学生怎么用宛如造句?