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