Ex5 More Variables and Printing

字符串和变量

Ex 5中,引入了格式化字符串的新东西(format string). 单纯的string是比较简单的: 用单引号或双引号包裹一端文本,就是 string 了。在示例代码里,要在一段文本里加上 variables,然后还要在最终显示结果里,把 variables 表示的内容一同显示出来。

Ex5 示例代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
my_name = 'Zed A. Shaw'
my_age = 35 # not a lie
my_height = 74 # inches
my_weight = 180 # lbs
my_eyes = 'Blue'
my_teeth = 'White'
my_hair = 'Brown'

print "Let's talk about %s." % my_name
print "He's %d inches tall." % my_height
print "He's %d pounds heavy." % my_weight
print "Actually that's not too heavy."
print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
print "His teeth are usually %s depending on the coffee." % my_teeth
# This line is tricky, try to get it exactly right
print "If I add %d, %d, and %d I get %d." %(my_age,
my_height, my_weight, my_age + my_height + my_weight)

运行结果如下:

新技能

Ex 5 里面, 要学习的新技能: format string%s, %d,%r 含义, string 中有多个变量时,该如何处理…

Licensed under CC BY-NC-SA 4.0
使用 Hugo 构建
主题 StackJimmy 设计