习题6、字符串(string)和文本
#!/usr/bin/python
# -*- coding:utf-8 -*-
x = "There are %d types of people." % 10
binary = "binary"
do_not = "don't"
y = "Those who konw %s and those who %s." % (binary, do_not)
print x
print y
print "I said: %r." % x
print "I also said: '%s'." %y
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"
print joke_evaluation % hilarious
w = "This is the left side of..."
e = "a string with a right side."
print w + e
小结
%r
和%s
有什么不同?
%r
用来做 debug 比较好,因为它会显示变量的原始数据(raw data),而其它的符号则是用来向用户显示输出的。- 既然有
%r
了,为什么还要用%s
和%d
?
%r
用来 debug 最好,而其它格式符则是用来向用户显示输出的。- 如果你觉得很好笑,可不可以写一句
hilarious = True
?
可以。在习题 27 中你会学到关于布尔函数的更多知识。- 为什么你在有些字符串上用了
‘
(单引号) 而在别的上没有用?
很大程度上只是个风格问题,我的风格就是在双引号的字符串中使用单引号。看看第 10 行。
习题7、更多打印
#!/usr/bin/python
# -*- coding:utf-8 -*-
print "Mary had a little lamb."
print "Its fleece was white as %s." % 'snow'
print "And everywhere that Mary went."
print "." * 10 # What'd that do ?
end1 = "C"
end2 = "h"
end3 = "e"
end4 = "e"
end5 = "s"
end6 = "e"
end7 = "B"
end8 = "u"
end9 = "r"
end10 = "g"
end11 = "e"
end12 = "r"
# watch that comma at the end. try removing it to see what happens
print end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 + end10 + end11 + end12
小结
- “end”语句是什么原理?
没有什么end语句,只是变量名里带了个 end 而已。- 为什么要用一个叫
‘snow’
的变量?
其实不是变量,而是一个带 snow 的字符串而已。变量时不会带引号的。- 你在加分习题 1 里说在每行代码上面写注解,一定要这样做吗?
不是。一般情况下加注解只是为了解释难懂的代码,或者注明为什么代码要这么写。一般来说后者
更为重要。碰到特殊情况你的代码的确每一行都很难懂的话,加注解也是正确的选择。在这里,我
主要是为了让你逐渐学会将代码翻译成日常语言- 创建字符串时是不是单引号和双引号都可以,它们有什么不同用途吗?
Python 里边两种都是可以的,不过一般单引号会被用来创建简短的字符串,例如‘a’
、‘snow’
这样的。- 不可以用逗号 , 将最后两行写成一行输出吗?
当然可以,不过这样以来这行的长度就超过 80 个字符了,这样做不是好的 Python 代码风格。
习题8、打印,打印
#!/usr/bin/python
# -*- coding:utf-8 -*-
formatter = "%r %r %r %r"
print formatter % (1, 2, 3, 4)
print formatter % ("one","two","three","four")
print formatter % (True, False, False, True)
print formatter % (formatter, formatter, formatter, formatter)
print formatter % (
"I had this thing.",
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight."
)
小结
- 我应该使用
%s
还是%r
?
你应该使用%s
,只有在想要获取某些东西的 debug 信息时才能用到%r
。%r
给你的是变量的“程序员原始版本”,又被称作“representation”。- 为什么
“one”
要用引号,而True
和False
不需要?
因为True
和False
是 Python 的关键字,用来表示真假的意义。如果你加了引号,它们就变成了字符串,也就无法实现它们本来的功能了。习题 27 中会有详细说明。- 我在字符中包含了中文(或者其它非 ASCII 字符),可是 %r 打印出的是乱码?
使用 %s 就行了。- 为什么
%r
有时打印出来的是单引号,而我实际用的是双引号?
Python 会用最有效的方式打印出字符串,而不是完全按照你写的方式来打印。这样做对于 %r 来
说是可以接受的,因为它是用作 debug 和排错,没必要非打印出多好看的格式。- 为什么 Python 3 里这些都不灵?
别使用 Python 3 系列。使用 Python 2.7 或更新的版本,虽然 Python 2.6 应该也没问题。
习题9、打印,打印,打印
#!/usr/bin/python
# -*- coding:utf-8 -*-
# Here's some new strange stuff, remember type it excatly.
days = "Mon Tue Wed Thu Fri Sat Sun"
months = "Jan\nFeb\nMar\nApr\nJun\nJul\nAug"
print "Here are the days: ", days
print "Here are the months: ", months
print """
There's something going on here.
With the three double_quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""
小结
- 怎样将月份显示在新的一行?
字符串以\n
开始就可以了,像这样:
"\nJan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"
- 为什么使用
%r
时\n
新行就不灵了?
%r
就是这个样子,它打印出的是你写出来的方式(或者近似方式)。它是用来 debug 的原始格
式。- 为什么在三引号之间加入空格就会出错?
你必须写成"""
而不是" " "
,引号之间不能有空格。- 为什么你打印时用了 + 而不是逗号?
因为我的目的是将两个字符串连接起来,组建成一个新的字符串。后面你会学到,print 里的逗号
其实是分隔参数的一种方式。
习题10、那是什么?
#!/usr/bin/python
# -*- coding:utf-8 -*-
tabby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslash_cat = "I'm \\ a \\ cat."
fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""
print tabby_cat
print persian_cat
print backslash_cat
print fat_cat
小结
\\
和别符号相比的有什么特别之处吗?
并无特别,这样只是为了输出一个反斜杠 (),想想为什么要把它写成两杠。//
和/n
怎么不灵?
因为你用了斜杠 / 而不是反斜杠 \,它们是不一样的字符,功能也完全不同。- 使用了
%r
后转义序列都不灵了。
因为%r
打印出的是你写到代码里的原始字符串,其中会包含原始的转义字符。你应该使用%s
,记住这条:%r
用作 debug,%s
用作显示。- 加分习题 #3 说是要组合什么的,是什么意思?
我想让你明白的一点是,所有这些习题中教你的东西都可以组合起来帮你解决问题。把你学过的格
式化字符串的知识和你新学到的转义字符的只是组合起来,写一些代码。'''
和"""
哪个好?
风格问题。现在你就用'''
吧,以后碰到再说。有时候用某一种可能会更美观,有时候你要遵循之前的写法从而让整个项目代码风格一致,看具体情况吧。
Python 格式化字符
Python转义字符