• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

关于python:Print输出函数怎么使用关于Print函数构造和Print函数格式化输出

python 搞代码 4年前 (2022-02-20) 20次浏览 已收录 0个评论

咱们在之前的文章中咱们用的最多的就是print()这个函数来打印一些数据,这就是咱们明天要讲的输入语句,通过print()不仅能够输入变量,还有很多其余性能。上面就来具体解说一下。

一、print()函数的结构

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

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

a = 3
c = 'python自学网'
e = 'python'print(c*a, e)print(c)
返回后果:
python自学网python自学网python自学网 python
python自学网

大家能够看进去两行打印代码会主动换行,咱们也能够通过其余办法自定义结尾的格局。

a = 3
c = 'python自学网'
e = 'python'print(c*a, e, end="")print(c)
返回后果:python自学网python自学网python自学网 pythonpython自学网

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

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

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


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:关于python:Print输出函数怎么使用关于Print函数构造和Print函数格式化输出
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址