Python 3.x下消除print()自动换行

本文介绍如何在Python3.x中使用print()函数输出时不自动换行。通过设置print()函数的关键参数end=' ',可以使输出内容在同一行显示。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Python 2.x下的print语句在输出字符串之后会默认换行,如果不希望换行,只要在语句最后加一个“,”即可。但是在Python 3.x下,print()变成内置函数,加“,”的老方法就行不通了。

        查询Python的Library Reference>Built-in Functions,找到如下条目

        “print([object...], *sep=' 'end='\n'file=sys.stdout)

Print object(s) to the stream file, separated by sep and followed by endsependand file, 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 objectis given, print() will just write end.

The file argument must be an object with a write(string) method; if it is not present or Nonesys.stdout will be used. ”

        其中,sep=''和end='\n'均是print()的关键参数,sep的默认值为空,end默认值为换行符,这就是print()在输出后默认换行的原因。相应的,解决办法就是对end赋值:print(something, something,.., end=''),使end值为空,这个换行就消除了。

 

原文地址:http://wpp9977777.blog.163.com/blog/static/46251007201192905622402/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值