笨方法学Python(6-10)
2015-12-30 09:30
513 查看
习题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转义字符
相关文章推荐
- python UserBasedCF
- python Item_CF
- python3使用了更多内存优化的技巧,比如,python3的zip就是生成可迭代的对象
- Python Network Programming(7)---泊松分布与发包
- Python 字符串编码中中文字符注意事项
- Python标准库08 多线程与同步 (threading包)
- Python For MySQL 使用连接器连接 ( 一)
- 如何解决Windows下sublime text2不能运行python的问题
- Python学习笔记3-Python元组、数组、字典集合的操作
- Python学习笔记3-Python元组、数组、字典集合的操作
- 初试Python爬虫-乌云厂商爬取
- Python 功能和特点(新手必学)
- APScheduler(Advance Python Scheduler) ImportError:
- python-66:BS4实例--下载BS4官方文档
- python-67:BS4简单浏览数据的方法
- python函数入参理解
- 最新win10下安装Scrapy 及其环境搭建
- Python公开课视频课程-基础篇
- Python公开课不得不去!
- python之编写购物车(第二天)