笨办法学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 的含义,了解 \ \ 的含义

首先来了解一下两种让字符串扩展到多行的方法:

  1. 换行符 \n (back-slash n ):两个字符的作用是在该位置上放入一个“新行(new line)”字符
  2. 双反斜杠(double back-slash) \ \ :这两个字符组合会打印出一个反斜杠来

3.1、转义序列:

下面介绍下再Python中常见的转义序列:

转义字符 描述
\ (在行尾时) 续行符
\ \ 反斜杠符号
' 单引号
" 双引号
\a 响铃
\b 退格(Backspace)
\e 转义
\000
\n 换行
\v 纵向制表符
\t 横向制表符
\r 回车
\f 换页
\oyy 八进制数yy代表的字符,例如:\o12代表换行
\xyy 十进制数yy代表的字符,例如:\x0a代表换行
\other 其它的字符以普通格式输出

在字符串中,有时需要包含一些特殊的符号,但是有些符号不能直接输出,就需要使用转义序列

举个栗子:

在打印输出一句话时,可能同时包含单引号和双引号,这种情况下在print 语句中不加其他操作肯定是会出错的。/手动滑稽

1547695833310

1547696182129

在这种情况下,我们有两种方法解决此问题;

  1. 使用转义序列
  2. 使用注释符-三引号

使用转义序列:

1547696840242

使用注释符:

1547696612939

总结:

转义序列就是将在print 下无法正常显示的字符打印出来,比如说打印 \ , 换行等。

再来认识一下转义字符 \b 的作用:作用是退格,就是删除前一个字符的意思

1547697550481

转义字符 \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 的作用。

posted @ 2019-01-17 13:54  无缘の邂逅#  阅读(738)  评论(0编辑  收藏  举报