【100+ python基础入门-12】print()输出函数

我们在之前的文章中我们用的最多的就是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 = 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()函数还能传入文件对象,这里就不多做演示了,在后面的文件读写中我们来细细品尝。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王子玉博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值