python print 输出详解

简介: python print 输出详解

print

eval()

>>> help(eval)      #这个是一招鲜,凡是不理解怎么用,就用这个看文档

Help on built-in function eval in module __builtin__:

eval(...)
    eval(source[, globals[, locals]]) -> value

    Evaluate the source in the context of globals and locals.
    The source may be a string representing a Python expression
    or a code object as returned by compile().
    The globals must be a dictionary and locals can be any mapping,
    defaulting to the current globals and locals.
    If only globals is given, locals defaults to it.

概括一下,eval()是把字符串中符合python表达式的东西计算出来。意思就是:

>>> 3+4         #这是一个表达式,python会根据计算法则计算出结果来
7
>>> "3+4"       #这是一个字符串,python就不计算里面的内容了,虽然里面是一个符合python规范的表达式
'3+4'
>>> eval("3+4") #这里就跟上面不一样了,就把字符串里面的表达式计算出来了
7

下面再看一个字符串“相加”的例子:

>>> "hiekay"+".github.io"
'hiekay.github.io'
>>> "'hiekay'+'.github.io'"    #字符串里面,python是不会进行“计算”的
"'hiekay'+'.github.io'"
>>> eval("'hiekay'+'.github.io'") #eval()做的事情完全不一样,它会把字符串里面的计算出来
'hiekay.github.io'

顺便再说一下另外一个跟eval()有点类似的函数:exec(),这个函数专门来执行字符串或文件里面的python语句。

>>> exec "print 'hello, hiekay'"
hello, hiekay
>>> "print 'hello, hiekay'"
"print 'hello, hiekay'"

print详解

print命令在编程实践中用的比较多,特别是要向看看程序运行到某个时候产生了什么结果了,必须用print来输出,本讲更宽泛地说,就要说明白把程序中得到的结果输出问题。

比较简单的输出,前面已经涉及到过了:

>>> name = 'hiekay'
>>> room = 703
>>> website = 'hiekay.github.io'
>>> print "MY name is:%s\nMy room is:%d\nMy website is:%s"%(name,room,website)
MY name is:hiekay
My room is:703
My website is:hiekay.github.io

其中,%s,%d就是占位符。

>>> a = 3.1415926
>>> print "%d"%a    #%d只能输出整数,int类型
3
>>> print "%f"%a  #%f输出浮点数
3.141593
>>> print "%.2f"%a #按照要求输出小数位数
3.14
>>> print "%.9f"%a  #如果要求的小数位数过多,后面就用0补全
3.141592600
>>> b = 3          
>>> print "%4d"%b   #如果是整数,这样写要求该整数占有四个位置,于是在前面增加三个空格
   3                #而不是写成0003的样式

换一种范式,写成这样,就跟上面有点区别了。

>>> import math     #引入数学模块
>>> print "PI=%f"%math.pi #默认,将圆周率打印成这个样子
PI=3.141593
>>> print "PI=%10.3f"%math.pi #约束一下,这个的含义是整数部分加上小数点和小数部分共计10位,并且右对齐
PI=     3.142
>>> print "PI=%-10.3f"%math.pi #要求显示的左对齐,其余跟上面一样
PI=3.142
>>> print "PI=%06d"%int(math.pi) #整数部分的显示,要求共6位,这样前面用0补足了。
PI=000003

其实,跟对上面数字操作类似,对字符串也可以做一些约束输出操作。

>>> website
'hiekay.github.io'
>>> print "%.3s"%website
hie
>>> print "%.*s"%(3,website)
hie
>>> print "%7.3s"%website
    hie
>>> print "%-7.3s"%website
hie    

总体上,跟对数字的输出操作类似。不过,在实际的操作中,这些用的真的不是很多,至少在我这么多年的代码生涯中,用到上面复杂操作的,就是现在给列位展示的时候,充其量用一用对float类型的数据输出小数位数的操作,其它的输出操作,以默认的那种方式居多。请看官在这里鄙夷我的无知吧。

行文到此,提醒列位,如果用python3的,请用print(),要加个括号。

print有一个特点,就是输出的时候,每行后面都自动加上一个换行符号n,这个在前面已经有所提及。

>>>  website
'hiekay.github.io'
>>> for word in website.split("."):
...     print word
... 
hiekay
github
io
>>> for word in website.split("."):
...     print word,         #注意,加了一个逗号,输出形式就变化了吧。
... 
hiekay github io

%r是万能的吗?

我曾经说过,懒人改变世界,特别是在敲代码的领域。于是就有人问了,前面一会儿是%s,一会儿是%d,麻烦,有没有一个万能的?于是网上就有人给出答案了,%r就是万能的。看实验:

>>> import math
>>> print "PI=%r"%math.pi
PI=3.141592653589793
>>> print "Pi=%r"%int(math.pi)
Pi=3

真的是万能呀!别着急,看看这个,你是不是就糊涂了?

>>> print "Pi=%s"%int(math.pi)
Pi=3

当然,这样就肯定出错了:

>>> print "p=%d"%"pi"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: %d format: a number is required, not str

如果看到这里,看官有点糊涂是很正常的,特别是那个号称万能的%r和%s,怎么都能够对原本属于%d的进行正常输出呢?

其实,不管是%r还是%s(%d)都是把做为整数的对象转化为字符串输出了,而不是输出整数。但是%r和%s是有点区别的,本讲对这个暂不做深入研究,只是说明这样的对应:%s-->str();%r-->repr(),什么意思呢?就是说%s调用的是str()函数把对象转化为str类型,而%r是调用了repr()将对象转化为字符串。关于两者的区别请参考:Difference between str and repr in Python,下面是一个简单的例子,演示一下两者区别:

>>> import datetime
>>> today = datetime.date.today()
>>> today
datetime.date(2014, 8, 15)
>>> str(today)
'2014-08-15'
>>> repr(today)
'datetime.date(2014, 8, 15)'

最后要表达我的一个观点,没有什么万能的,一切都是根据实际需要而定。

关于更多的输出格式占位符的说明,这个页面中有一个表格,可惜没有找到中文的,如果看官找到中文的,请共享一下呀:string formatting

再扩展

>>> myinfo
{'website': 'hiekay.github.io', 'name': 'hiekay', 'room': 703}
>>> print "hiekay is in %(room)d"%myinfo
hiekay is in 703

看官是否看明白上面的输出了?有点意思。这样的输出算是对前面输出的扩展了。

出了这个扩展之外,在输出的时候,还可以用一个名曰:format的东西,这里面看不到%,但是多了{}。看实验先:

>>> print "My name is {0} and I am in {1}".format("hiekay",703)     #将format后面的内容以此填充
My name is hiekay and I am in 703
>>> "My website is {website}".format(website="hiekay.github.io")    #{}里面那个相当于一个变量了吧
'My website is hiekay.github.io'

format输出方式牛逼.

目录
相关文章
|
1月前
|
存储 缓存 程序员
Python程序员Debug利器,和Print说再见
Python程序员Debug利器,和Print说再见
25 2
|
4月前
|
存储 IDE 开发工具
【python原理】Python 3里面print为什么改成函数?为什么会有个奇怪的“...”对象?为什么推荐蛇形命名法?等常见问题
【python原理】Python 3里面print为什么改成函数?为什么会有个奇怪的“...”对象?为什么推荐蛇形命名法?等常见问题
46 0
|
5月前
|
Python
python pyqt5 cmd 命令行 控制台 打印 print 输出 显示打印内容 实时显示 界面
python pyqt5 cmd 命令行 控制台 打印 print 输出 显示打印内容 实时显示 界面
167 0
|
1月前
|
索引 Python
Python入门05 print函数
Python入门05 print函数
|
2月前
|
程序员 C语言 Python
[oeasy]python0007_ print函数_字符串_display_电传打字机_程序员的浪漫
[oeasy]python0007_ print函数_字符串_display_电传打字机_程序员的浪漫
37 5
[oeasy]python0007_ print函数_字符串_display_电传打字机_程序员的浪漫
|
3月前
|
C# Python
C# 笔记3 - 重载一系列像python那样的print()方法
C# 笔记3 - 重载一系列像python那样的print()方法
27 1
|
4月前
|
Python
【python】print函数从python2转换为python3形式
【python】print函数从python2转换为python3形式
32 0
|
4月前
|
IDE 前端开发 Linux
Python 教程之输入输出(9)—— print() 中的 sep 参数
Python 教程之输入输出(9)—— print() 中的 sep 参数
97 0
|
4月前
|
前端开发 程序员 C++
Python 教程之输入输出(8)—— print() 中的 Python 结束参数
Python 教程之输入输出(8)—— print() 中的 Python 结束参数
57 0
|
4月前
|
测试技术 Python
Python 教程之输入输出(6)—— 使用 print() 函数输出
Python 教程之输入输出(6)—— 使用 print() 函数输出
150 0