笨办法学Python - 习题8-10: Printing & Printing, Printing
目录
1、习题 8: 打印,打印
学习目标:继续学习 %r 的格式化输出。
习题八中的练习代码是:
#! -*-coding=utf-8 -*- formatter = "%r %r %r %r %r " print formatter % (1, "hello", [1,2,3], (1,2,3), {"name":"jack"}) print formatter % ("one", "two", "three", "four", "five") print formatter % (true, false, true, false, false) print formatter % ( "i had this thing. ", "that you could type up right. ", "but it didn't sing. ", "so i said doognight. ", "hello world." )
上述代码的运行结果是:
c:\python27\python.exe d:/pythoncode/stupid_way_study/demo8/exer8-1.py 1 'hello' [1, 2, 3] (1, 2, 3) {'name': 'jack'} 'one' 'two' 'three' 'four' 'five' true false true false false 'i had this thing. ' 'that you could type up right. ' "but it didn't sing. " 'so i said doognight. ' 'hello world.' process finished with exit code 0
注意:上述代码说明两个点,一个是%r 的作用,是占位符,可以将后面给的值按原数据类型输出(不会变),支持数字、字符串、列表、元组、字典等所有数据类型。
还有一个需要注意的就是代码的最后一行:
print formatter % ( "i had this thing. ", "that you could type up right. ", "but it didn't sing. ", "so i said doognight. ", "hello world." )
'i had this thing. ' 'that you could type up right. ' "but it didn't sing. " 'so i said doognight. ' 'hello world.'
最后输出的语句中既有单引号,也有双引号。原因在于 %r 格式化字符后是显示字符的原始数据。而字符串的原始数据包含引号,所以我们看到其他字符串被格式化后显示单引号。 而这条双引号的字符串是因为原始字符串中有了单引号,为避免字符意外截断,python 自动为这段字符串添加了双引号。
2、习题 9: 打印,打印,打印
学习目标:了解 \n 的含义
习题九中的练习代码是:
#! -*-coding=utf-8 -*- days = "mon tue wed thu fri sat sun" months = "jan\nfeb\nmar\napr\nmay\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. """
c:\python27\python.exe d:/pythoncode/stupid_way_study/demo9/exer9-1.py here are the days: mon tue wed thu fri sat sun here are the months: jan feb mar apr may jun jul aug 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. process finished with exit code 0
上述代码有两个点需要注意下,一个是换行符 \n ,一个是注释符三引号。换行符就是避免代码过长影响阅读性而手动进行代码换行操作,\n 其实只是一个字符,类似的还有制表符 \t ,具体的更过的换行符知识请见下一题。
3、习题 10: 那是什么?
学习目标:了解 \n 的含义,了解 的含义
首先来了解一下两种让字符串扩展到多行的方法:
- 换行符 \n (back-slash n ):两个字符的作用是在该位置上放入一个“新行(new line)”字符
- 双反斜杠(double back-slash) :这两个字符组合会打印出一个反斜杠来
3.1、转义序列:
下面介绍下再python中常见的转义序列:
转义字符 | 描述 |
---|---|
(在行尾时) | 续行符 |
\ | 反斜杠符号 |
' | 单引号 |
" | 双引号 |
\a | 响铃 |
\b | 退格(backspace) |
\e | 转义 |
\000 | 空 |
\n | 换行 |
\v | 纵向制表符 |
\t | 横向制表符 |
\r | 回车 |
\f | 换页 |
\oyy | 八进制数yy代表的字符,例如:\o12代表换行 |
\xyy | 十进制数yy代表的字符,例如:\x0a代表换行 |
\other | 其它的字符以普通格式输出 |
在字符串中,有时需要包含一些特殊的符号,但是有些符号不能直接输出,就需要使用转义序列
举个栗子:
在打印输出一句话时,可能同时包含单引号和双引号,这种情况下在print 语句中不加其他操作肯定是会出错的。/手动滑稽
在这种情况下,我们有两种方法解决此问题;
- 使用转义序列
- 使用注释符-三引号
使用转义序列:
使用注释符:
总结:
转义序列就是将在print 下无法正常显示的字符打印出来,比如说打印 , 换行等。
再来认识一下转义字符 \b 的作用:作用是退格,就是删除前一个字符的意思
[1547697550481]( " \b作用 - 退格、删除")
转义字符 \r :也是换行作用,与 \n 不同的是光标的位置:\n 在下一行开头,\r 在本行的开头
print u"你好吗?\n朋友" print u"——分隔线——" print u"你好吗?\r朋友" print "hello \rworld"
c:\python27\python.exe d:/pythoncode/stupid_way_study/demo10/exer10-1.py 你好吗? 朋友 ——分隔线—— 朋友 world process finished with exit code 0
从上面代码可以看出来,\r 是回车,是只会打印\r 后面的内容,前面的内容自动忽略。
具体的其他制表符运用还得自己练习。
习题十中的练习代码是:
#! -*-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
上述代码的运行结果为:
c:\python27\python.exe d:/pythoncode/stupid_way_study/demo10/exer10-1.py i'm tabbed in. i'm split on a line. i'm \ a \ cat. i'll do a list: * cat food * fishies * catnip * grass process finished with exit code 0
从上面可以看出转义字符的含义。 t 是水平制表符, 是用于打印 的。
如果将转义字符和格式化输出相结合,则会生成一个更复杂的格式,举个栗子:
fat_cat = """ i'll do a list: \t* cat food \t* fishies \t* catnip\n\t* grass """ print "heloo %r " % fat_cat print "----------------------------" print "heloo %s " % fat_cat
c:\python27\python.exe d:/pythoncode/stupid_way_study/demo10/exer10-1.py heloo "\ni'll do a list:\n\t* cat food\n\t* fishies\n\t* catnip\n\t* grass\n" ---------------------------- heloo i'll do a list: * cat food * fishies * catnip * grass process finished with exit code 0
从上面的代码中可以更好的体现出格式化输出的占位符 %r 和 %s 之间的区别。%r 是输出原格式,%s是输出字符串。
4、习题总结:
上面的三道习题,前两题只是之前的知识回顾,就是格式化输出的应用实践,后面习题10是说明了常见转义字符的作用,和一些续航建的转义字符的含义。结合格式化输出和转义字符可以生成更复杂的格式。重点理解%s 和 %r 的作用。
上一篇: 都是女人,何必以大欺小
下一篇: 一张图教会你什么是“吃和碰”
推荐阅读