字符串的合并和占位符的使用
注意在%号前不能有引号。
-
1 # -*- coding:utf-8 -*- 2 my_name ='中国' 3 my_age =35 4 my_height =74 5 my_weight =180 6 my_teeth ='White' 7 my_hair ='Brown' 8 print("Let's talk about %s."% my_name) 9 print("He's %d inch tall, %s"%(my_height, my_name)) 10 #test注意 11 print("Let's add my height %d to my weight %d,and get the result: %d"%(my_height,my_weight,my_height+my_weight))
另一个例子:
-
1 x ="There are %d types of people"%10 2 binary ="binary" 3 do_not ="don't" 4 y="Those who know %s and those who %s."%(binary,do_not) 5 print x 6 print y 7 print"I said:%r"%x 8 print"I also said: '%s'."% y 9 hilarious =False 10 joke_evaluation ="Isn't that Joke so funny?! %r" 11 print joke_evaluation %hilarious 12 w="This is the left side of ..." 13 e="a string with a right side." 14 print w+e
运行结果:
There are 10 types of people
Those who know binary and those who don't.
I said:'There are 10 types of people'
I also said: 'Those who know binary and those who don't.'.
Isn't that Joke so funny?! False
This is the left side of ...a string with a right side.