Python中使用print()时如何实现不换行

平时刷题的时候大家可能会发现打印字符的时候需要你不换行才能得到正确答案,那么如何实现的。下面直接看例子。

使用print()函数时其实还有个默认的参数end,来看看具体怎么回事

python 复制代码
list = ['a','b','c']
for i in list:
    print(i)

打印结果:

这是不加参数的情况,就是直接换行了,接下来看看加入end参数后

python 复制代码
list = ['a','b','c']
for i in list:
    # print(i)
    print(i,end='\n')#默认是end='\n',输入与否和平时一样

打印结果:

因为end=\n时默认值,所以还是一样,要想在一行打印出来,需要设置end=""或者end=" ",后者是加入了空格

python 复制代码
list = ['a','b','c']
for i in list:
    # print(i)
    # print(i,end='\n')#默认是end='\n',输入与否和平时一样
    print(i,end='')

打印结果:

python 复制代码
list = ['a','b','c']
for i in list:
    # print(i)
    # print(i,end='\n')#默认是end='\n',输入与否和平时一样
    # print(i,end='')
    print(i, end=' ')

打印结果:

甚至还可以让end=其他字符,比如end='+'

python 复制代码
list = ['a','b','c']
for i in list:
    # print(i)
    # print(i,end='\n')#默认是end='\n',输入与否和平时一样
    # print(i,end='')
    print(i, end='+')

打印结果:

相关推荐
二十雨辰21 小时前
[python]-AI大模型
开发语言·人工智能·python
Yvonne爱编码21 小时前
JAVA数据结构 DAY6-栈和队列
java·开发语言·数据结构·python
前端摸鱼匠1 天前
YOLOv8 环境配置全攻略:Python、PyTorch 与 CUDA 的和谐共生
人工智能·pytorch·python·yolo·目标检测
WangYaolove13141 天前
基于python的在线水果销售系统(源码+文档)
python·mysql·django·毕业设计·源码
AALoveTouch1 天前
大麦网协议分析
javascript·python
ZH15455891311 天前
Flutter for OpenHarmony Python学习助手实战:自动化脚本开发的实现
python·学习·flutter
xcLeigh1 天前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests
xcLeigh1 天前
Python入门:Python3 statistics模块全面学习教程
开发语言·python·学习·模块·python3·statistics
YongCheng_Liang1 天前
从零开始学 Python:自动化 / 运维开发实战(核心库 + 3 大实战场景)
python·自动化·运维开发
鸽芷咕1 天前
为什么越来越多开发者转向 CANN 仓库中的 Python 自动化方案?
python·microsoft·自动化·cann