网站首页 > 技术文章 正文
Python中的print()函数功能是用于打印输出,是编程语言中最常见的一个函数。我们一般使用print()输出字符串和变量的值。
学习一门程序设计语言,第一个演示程序就是输出“hello world!”,这已经是一种习惯。在Python中我们可以用如下代码实现。
print(“hello world!”)
这只是print()最基本的使用方法。详细的语法格式如下。
print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)
参数:
objects -- 输出对象,输出多个对象时,需要用逗号分隔。
sep -- 分隔多个对象,默认是一个空格。
end -- 设定输出以什么结尾。默认值是换行符 \n。
file -- 要写入的文件对象。
flush -- 输出是否被刷新。
实例说明:
>>> #输出数值、字符串
>>> print(100)
100
>>> print("hello world!")
hello world!
>>> #输出变量的值
>>> n=100
>>> print(n)
100
>>> #输出多个对象,用逗号分隔
>>> print("hello","world",n)
hello world 100
>>> #输出结果用*分隔
>>> print("hello","world",n,sep="*")
hello*world*100
#print()默认输出后自动换行
#end=""可以将最后的换行符替换为空,也可已替换为其他转义字符
#直接使用print(),输出为空,只起换行作用
print("hello")
print("world")
print()
print("hello",end="")
print("world")
#输出结果
hello
world
helloworld
#直接将输出写入文件
fw=open("abc.txt","w")
print("abcdefg",file=fw)
fw.close()
上面写入文件的代码如果去掉fw.close(),我们在abc.txt中看不到内容,也就是输出内容了,但没及时刷新。flush=True参数可以在不使用fw.close()时看到文件内容,也就是print输出后即时刷新。
fw=open("abc.txt","w")
print("abcdefg",file=fw,flush=True)
我们利用输出及时刷新特性可以实现Loading......动态效果。
import time
print("Loading",end="")
for i in range(6):
print(".",end="",flush=True)
time.sleep(1)
感谢你的阅读,关注我,精彩继续!
猜你喜欢
- 2024-10-01 利用神经网络模型检测摄像头上的可疑行为
- 2024-10-01 使用神经网络的自动化特征工程(神经网络的特点及使用场景)
- 2024-10-01 Python基础学习必备的8个最常用的内置函数
- 2024-10-01 利用Click和argparse给你Python程序构建一个优雅的命令行界面
- 2024-10-01 langchain中的LLM模型使用介绍(llvm 分析)
- 2024-10-01 学习Python内置函数(range)来打印数学乘法表
- 2024-10-01 Python 100天 15:print("hello world")茴香豆的写法
- 2024-10-01 python3入门实例一:Hello World(python的hello world程序编写)
- 2024-10-01 python基础篇:讲讲python的内置函数一
- 2024-10-01 Python3中的print函数(python里的print函数)
- 最近发表
- 标签列表
-
- cmd/c (57)
- c++中::是什么意思 (57)
- sqlset (59)
- ps可以打开pdf格式吗 (58)
- phprequire_once (61)
- localstorage.removeitem (74)
- routermode (59)
- vector线程安全吗 (70)
- & (66)
- java (73)
- org.redisson (64)
- log.warn (60)
- cannotinstantiatethetype (62)
- js数组插入 (83)
- resttemplateokhttp (59)
- gormwherein (64)
- linux删除一个文件夹 (65)
- mac安装java (72)
- reader.onload (61)
- outofmemoryerror是什么意思 (64)
- flask文件上传 (63)
- eacces (67)
- 查看mysql是否启动 (70)
- java是值传递还是引用传递 (58)
- 无效的列索引 (74)