python输出函数叫什么
在编程实践中,print的使用频率非常高,特别是程序运行到某个时刻,要检测产生的结果时,必须用print来打印输出。
关于print函数,前面很多地方已经提及过,可用于写入标准输出。现在,是时候该深入了。
注意:这里强调的是“print函数”,而不是“print语句”。
深入print
在Python2.x中,print是一个语句,但是在Python3.x中,它是一个函数。如果2.x和3.x都使用过,你就会发现差异有多么大。
进入3.x的交互式shell,尝试使用“print语句”:
[wang@localhost~]$python
Python3.5.2(default,Mar292017,11:05:07)
[GCC4.8.520150623(RedHat4.8.5-11)]onlinux
Type"help","copyright","credits"or"license"formoreinformation.
>>>
>>>print'Python'
...
SyntaxError:Missingparenthesesincallto'print'
对于大多数人来说,这个错误信息再熟悉不过了。正如上面所提到的那样,print是3.x中的一个函数,与其他函数一样,参数应该被圆括号括起来
>>>print('Python')
Python
print函数
要了解print函数的用途,可以使用help()来寻求帮助:
>>>help(print)
...
Helponbuilt-infunctionprintinmodulebuiltins:
print(...)
print(value,...,sep='',end='\n',file=sys.stdout,flush=False)
Printsthevaluestoastream,ortosys.stdoutbydefault.
Optionalkeywordarguments:
file:afile-likeobject(stream);defaultstothecurrentsys.stdout.
sep:stringinsertedbetweenvalues,defaultaspace.
end:stringappendedafterthelastvalue,defaultanewline.
flush:whethertoforciblyflushthestream.
将对象输出到文本流文件,由sep分开,然后以end结束。如果sep、end、file和flush出现,则必须以关键字参数的形式指定。
不使用关键字参数
print函数可以打印任意数量的值(value1,value2,…),这些值由逗号分隔。
>>>age=18
>>>
>>>print('age',age)
age18
很容易发现,两个值之间有一个分隔符-空格(默认值),这取决于sep。
分隔符
如果要重新定义分隔符,可以通过sep来指定。
>>>print('age',age,sep='')#去掉空格
age18
>>>
>>>print('www','python','org',sep='.')#以.分割
www.python.org
以上内容为大家介绍了python培训之输出函数叫什么,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

相关推荐HOT
更多>>
python正则表达式中的零宽断言
python正则表达式中的零宽断言1、概念有些元字符不匹配任何字符,只是简单的表示成功或失败,所以这些字符也叫零宽断言。2、符号举例(1)|或操作...详情>>
2023-11-14 11:35:03
python方法的绑定和未绑定
python方法的绑定和未绑定1、说明未绑定对象的方法:无self参数的方法,通过定义类调用函数,返回未绑定self的方法。绑定对象的方法:带self参...详情>>
2023-11-14 09:53:02
python海象运算符的使用
python海象运算符的使用1、在判断条件下允许操作。在一定程度上简化了代码,但降低了可读性。i=len((l:=[1,2,3]))#先对l进行赋值,在对i赋值whi...详情>>
2023-11-14 02:38:21
pythonelif语句报错是什么原因
python的else和elif语句也可以叫做子句,因为它们不能独立使用,两者都是出现在if、for、while语句内部的。else子句可以增加一种选择;而elif子...详情>>
2023-11-13 21:46:35热门推荐
技术干货






