一篇文章带你了解Python print() 函数

在这里插入图片描述

现如今,Python成了网络不可或缺的一部分,下面就给大家分享Python print() 函数的应用。

print(*objects, sep=’ ‘, end=’\n’, file=sys.stdout, flush=False)
将对象以字符串表示的方式格式化输出到流文件对象file里。其中所有非关键字参数都按str()方式进行转换为字符串输出;

关键字参数sep是实现分隔符,比如多个参数输出时想要输出中间的分隔字符;

关键字参数end是输出结束时的字符,默认是换行符\n;

关键字参数file是定义流输出的文件,可以是标准的系统输出sys.stdout,也可以重定义为别的文件;

关键字参数flush是立即把内容输出到流文件,不作缓存。

【例子】没有参数时,每次输出后都会换行。

shoplist = [‘apple’, ‘mango’, ‘carrot’, ‘banana’]print(“This is printed without 'end’and ‘sep’.”)for item in shoplist:print(item)# This is printed without 'end’and ‘sep’.# apple# mango# carrot# banana
【例子】每次输出结束都用end设置的参数&结尾,并没有默认换行。

shoplist = [‘apple’, ‘mango’, ‘carrot’, ‘banana’]print(“This is printed with ‘end=’&’’.”)for item in shoplist:print(item, end=’&’)print(‘hello world’)# This is printed with ‘end=’&’’.# apple&mango&carrot&banana&hello world
【例子】item值与’another string’两个值之间用sep设置的参数&分割。由于end参数没有设置,因此默认是输出解释后换行,即end参数的默认值为\n。

shoplist = [‘apple’, ‘mango’, ‘carrot’, ‘banana’]print(“This is printed with ‘sep=’&’’.”)for item in shoplist:print(item, ‘another string’, sep=’&’)# This is printed with ‘sep=’&’’.# apple&another string# mango&another string# carrot&another string# banana&another string

以上就是关于Python print() 函数的相关内容介绍了,希望能够给大家带来帮助。
文章部分内容源于网络,联系侵删*
文章转自:http://h.jiguangdaili.com/news/92651.html

猜你喜欢

转载自blog.csdn.net/zhimaHTTP/article/details/113744491