## python-习题6~10 原

南风末

``````x = "There are %d types of people."%10
binary = "binary"
do_not= "don't"
y = "Those who know %s and those who %s."%(binary,do_not)  #定义变量，将要输出的内容存放到变量x，y，
#   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 the left side of……"
e = "a string with a right side."

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.'.   #  变量x中的""变成了''
I also said:'Those who know binary and those who don't.'.
Isn't that joke so funny?!False
This the left side of……a string with a right side.  #   两变量相加合成一句了。``````

1.  用“+”号可以把两个字符串连起来组成一个更长的字符串（运算符重载）。

2.  当变量x里存放的字符串含有“”时，输出时不需要再加“”。

3.  输出后变量x中的“”变成了''.

1.  给以上程序加注释。（已加）

2.  找到所以字符串包含字符串的位置，总共有四个位置。（如上：用序号①②③④⑤标注）

3.  你确定只有四个位置吗？你怎么知道的？没准我在骗你呢。（作者真调皮）

4.  解释一下为什么w和e用 + 连起来就可以生成一个更长的字符串。

运算符重载。具体说来，每个变量在Python中都是以对象形式存在的，即都是继承于Object。而Object则具有`object.__add__(self, other)`这样一个方法。每当处理`x+y`这一个表达式的时候，将会调用`x.__add__(y)`。另外为了交换律，还有`__radd__`方法，也就是用来处理`y+x`，这样会调用`y.__radd__(x)`

``````class Free:
def __init__(self, info):
self.info = info
return str(self) + moreinfo
return moreinfo + str(self)
def __str__(self):
return self.info
``````

`Free`这个Class，便得以直接成为`+`操作符的左/右参数。

Efficient String Concatenation in Python
The Python String Concatenation Shootout

``````print "Mary had a little lamb."
print "Its fleece was white as %s."% 'snow'  # 字符串变量用单引号括起来，区别于C
print "And everywhere that Mary went."
print "."* 10 # what'd that do?  ->输出10个“.”

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  # 运算符重载
``````

``````Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
cheese Burger``````

1.  字符串变量用单引号  ' '.

2.  运算符重载：end（1~12）组成长字符串。

3.  倒数第三行的逗号去掉结果会换行。

``````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 % (
"That you could type up right.",
"But it didn't sing.",
"So I said goodnight,"
)``````

``````1 2 3 4
'one' 'two' 'three' 'four'
True False False True
'I had this thing.' 'That you could type up right.' "But it didn't sing." 'So I said goodnight,'``````

1.  最后一行输出结果既含有单引号又含有双引号，

->字符串里面没有单引号时且外面是双引号时，输出单引号；

（外层是单引号时，输出时无引号；）

里层有单引号时，输出外层双引号。

``````# Here's some new strange stuff,remenber type it exactly.

days = "Mon Tue Wed Thu Fri Sat Sum"
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.
"""
``````

``````Here are the days: Mon Tue Wed Thu Fri Sat Sum
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.
``````

``````"I am 6'2\" tall,"  #将字符串中的双引号转义
'I am 6\'2" tall."  #将字符串中的单引号转义
``````

``````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 list:
\t* Cat food
\t*Fishies
\t*Catnip\n\t* Grass
"""

print tabby_cat
print persian_cat
print backslash_cat
print fat_cat
``````

``````	I'm tabbed in.
I'm split
on a line.
I'm \ a \ cat.

I'll do list:
* Cat food
*Fishies
*Catnip
* Grass
``````

1.  "\t"制表符，缩进4个空格。

2.  三引号""",转义，可以放入任意多行文字。

1.  另有Python转义字符：

print r'\t\r'

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

2.  使用三个单引号（' ' '）和三个双引号，效果是一样的。

3.  将转义序列和格式化字符串放到一起，创建一种更为复杂的格式。

4.  %r  打印出来的是你写在脚本里的内容，而%s  打印的是你应该看到的内容（换句话说%r会打印任意你放入的内容）

### 南风末

python核心编程6-14习题的解题思路修改版

python核心编程6-14习题的解题思路修改版 #coding:utf-8 """ python核心编程6-14习题的解题思路 设计一个"石头,剪子,布"游戏,有时又叫"Rochambeau",你小时候可能玩过,下面是规则.你和你的对手......

2014/02/10
0
1

2015/02/10
0
0
python3测试工具开发快速入门教程2数据结构1变量与赋值

2018/08/30
0
0

@fzyz_sb 你好，想跟你请教个问题： 你好，我这几天一直在学习《python核心编程》，在网上搜习题答案时找到的你的博客，所以这些天也一直是看着你的博客学习的。我计算机编程只是在学校开的c...

_keyboard_dancer_
2015/04/05
2.9K
3
《趣学Python编程》习题总结

2014/12/01
0
0

http://www.php.cn/jishu/php/413274.html php官网 下载php版本 https://windows.php.net/

15834278076
5分钟前
0
0

10分钟前
0
0
mysql5.7 [Err] 1118 - Row size too large (> 8126).

mysql [Err] 1118 - Row size too large (> 8126). 问题 mysql5.7 在执行创建表或者增加字段时，发现row size长度过长，导致出现以下错误。 [Err] 1118 - Row size too large (> 8126). C......

GritTan
11分钟前
0
0
[mybatis]一级缓存与二级缓存配置（mybatis3.4.0）

Danni3
21分钟前
0
0
Kafka两级调度实现分布式协调任务分配Golang版

8小时
28分钟前
5
0