笨办法学Python(五)

习题 5: 更多的变量和打印

    我们现在要键入更多的变量并且把它们打印出来。这次我们将使用一个叫“格式化字符串(format string)”的东西. 每一次你使用 " 把一些文本引用起来,你就建立了一个字符串。 字符串是程序将信息展示给人的方式。你可以打印它们,可以将它们写入文件,还可以将它们发送给网站服务器,很多事情都是通过字符串交流实现的。

    字符串是非常好用的东西,所以再这个练习中你将学会如何创建包含变量内容的字符串。使用专门的格式和语法把变量的内容放到字符串里,相当于来告诉 python :“嘿,这是一个格式化字符串,把这些变量放到那几个位置。”

    一样的,即使你读不懂这些内容,只要一字不差地键入就可以了。

 1 my_name = 'Zed A. Shaw' 
 2 my_age = 35 # not a lie 
 3 my_height = 74 # inches 
 4 my_weight = 180 # lbs 
 5 my_eyes = 'Blue' 
 6 my_teeth = 'White' 
 7 my_hair = 'Brown' 
 8 
 9 print "Let's talk about %s." % my_name 
10 print "He's %d inches tall." % my_height 
11 print "He's %d pounds heavy." % my_weight 
12 print "Actually that's not too heavy." 
13 print "He's got %s eyes and %s hair." % (my_eyes, my_hair) 
14 print "His teeth are usually %s depending on the coffee." % my_teeth
15 
16 # this line is tricky, try to get it exactly right 
17 print "If I add %d, %d, and %d I get %d." % ( my_age, my_height, my_weight, my_age + my_height + my_weight)
View Code

Warning

  如果你使用了非 ASCII 字符而且碰到了编码错误,记得在最顶端加一行 # -- coding: utf-8 -- 。

    你应该看到的结果:

加分习题

  1. 修改所有的变量名字,把它们前面的``my_``去掉。确认将每一个地方的都改掉,不只是你使用``=``赋值过的地方。

  2. 试着使用更多的格式化字符。例如 %r 就是是非常有用的一个,它的含义是“不管什么都打印出来”。

  3. 在网上搜索所有的 Python 格式化字符。

  4. 试着使用变量将英寸和磅转换成厘米和千克。不要直接键入答案。使用 Python 的计算功能来完成。

习题练习

1.

 1 name = 'Zed A. Shaw' 
 2 age = 35 # not a lie 
 3 height = 74 # inches 
 4 weight = 180 # lbs 
 5 eyes = 'Blue' 
 6 teeth = 'White' 
 7 hair = 'Brown' 
 8 
 9 print "Let's talk about %s." % name 
10 print "He's %d inches tall." % height 
11 print "He's %d pounds heavy." % weight 
12 print "Actually that's not too heavy." 
13 print "He's got %s eyes and %s hair." % (eyes, hair) 
14 print "His teeth are usually %s depending on the coffee." % teeth
15 
16 # this line is tricky, try to get it exactly right 
17 print "If I add %d, %d, and %d I get %d." % ( age, height, weight, age + height + weight)
View Code

3.

http://www.cnblogs.com/benric/p/4965224.html

http://www.cnblogs.com/wilber2013/p/4641616.html

    "%"是Python风格的字符串格式化操作符,非常类似C语言里的printf()函数的字符串格式化(C语言中也是使用%)。

格式化符号

说明

%c

转换成字符(ASCII 码值,或者长度为一的字符串)

%r

优先用repr()函数进行字符串转换

%s

优先用str()函数进行字符串转换

%d / %i

转成有符号十进制数

%u

转成无符号十进制数

%o

转成无符号八进制数

%x / %X

转成无符号十六进制数(x / X 代表转换后的十六进制字符的大小写)

%e / %E

转成科学计数法(e / E控制输出e / E)

%f / %F

转成浮点数(小数部分自然截断)

%g / %G

%e和%f / %E和%F 的简写

%%

输出% (格式化字符串里面包括百分号,那么必须使用%%)

    关于"%s"和"%r"的差别:

1 string = "helloworld
!	!"
2 print "%s" % string 
3 print "%r" % string

    其实,这里的差异是str()和repr()两个内建函数之间的差异:

  • str()得到的字符串是面向用户的,具有较好的可读性
  • repr()得到的字符串是面向机器的
  • 通常(不是所有)repr() 得到的效果是:obj == eval(repr(obj))

    通过"%"可以进行字符串格式化,但是"%"经常会结合下面的辅助符一起使用。

辅助符号

说明

*

定义宽度或者小数点精度

-

用做左对齐

+

在正数前面显示加号(+)

#

在八进制数前面显示零(0),在十六进制前面显示"0x"或者"0X"(取决于用的是"x"还是"X")

0

显示的数字前面填充"0"而不是默认的空格

(var)

映射变量(通常用来处理字段类型的参数)

m.n

m 是显示的最小总宽度,n 是小数点后的位数(如果可用的话)

    举例:

 1 #-- coding: utf - 8 --
 2 
 3 # 数制
 4 num = 1024
 5 print '%d is %i' %(num, num)
 6 
 7 print '%d is %u' %(num, num)
 8 
 9 print '%d is %o' %(num, num)
10 print '%d is %#o' %(num, num)
11 
12 print '%d is %x' %(num, num)
13 print '%d is %#x' %(num, num)
14 
15 # 浮点数
16 f = 3.14159265358
17 print 'pa = %.3f' %f
18 print 'pa = %8.3f' %f
19 
20 # 指定宽度和对齐
21 students = [{"name":"Wilber", "age":27}, {"name":"Will", "age":28}, {"name":"June", "age":27}]
22 print "name: %10s, age: %10d" %(students[0]["name"], students[0]["age"])
23 print "name: %-10s, age: %-10d" %(students[1]["name"], students[1]["age"])
24 print "name: %*s, age: %0*d" %(10, students[2]["name"], 10, students[2]["age"])
View Code

    注:当代码中有中文时,记得写 #-- coding: utf - 8 -- 不然会报错。

原文地址:https://www.cnblogs.com/yllinux/p/7061245.html