首页
学习
活动
专区
圈层
工具
发布
社区首页 >专栏 >Prin()输出函数和使用方法

Prin()输出函数和使用方法

原创
作者头像
python自学网
发布2021-11-29 14:23:05
发布2021-11-29 14:23:05
9400
举报

我们在之前的文章中我们用的最多的就是print()这个函数来打印一些数据,这就是我们今天要讲的输出语句,通过print()不仅可以输出变量,还有很多其他功能。下面就来详细讲解一下。

一、print()函数的构造

代码语言:python
复制
def print(self, *args, sep=' ', end='\n', file=None): # known special case of print
 """
 print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
 Prints the values to a stream, or to sys.stdout by default.
 Optional keyword arguments:
 file:  a file-like object (stream); defaults to the current sys.stdout.
 sep:   string inserted between values, default a space.
 end:   string appended after the last value, default a newline.
 flush: whether to forcibly flush the stream.
 """
 pass

通过上面的构造函数我们可以看出来,这个函数可以传入多个值,并且自带空格隔开每个变量,另外结尾会自带一个换行。下面我们来演示一下。

代码语言:python
复制
a = 3
c = 'python自学网'
e = 'python'print(c*a, e)print(c)
返回结果:
python自学网python自学网python自学网 python
python自学网

大家可以看出来两行打印代码会自动换行,我们也可以通过其他方法自定义结尾的格式。

代码语言:python
复制
a = 3
c = 'python自学网'
e = 'python'print(c*a, e, end="")print(c)
返回结果:python自学网python自学网python自学网 pythonpython自学网

二、print()函数格式化输出

代码语言:python
复制
a = 3
c = 'python自学网'
e = 'python'
f = 800print('网站名称:%s' % c) 
# 使用%s来替换字符串print('网站有视频教程:%d集以上' % f) 
# 使用%d来替换数字print('{}视频教程'.format(e)) 
# 使用format()函数来替换所有字符print(c, '\t', e) 
 # \t 表示空格print(c, '\n', e) # \n 表示换行
返回结果:
网站名称:python自学网
网站有视频教程:800集以上
python视频教程
python自学网      python
python自学网
 python

此外print()函数还能传入文件对象,这里就不多做演示了,在后面的文件读写中我们来细细品尝。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、print()函数的构造
  • 二、print()函数格式化输出
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档