笨办法学Python - 习题6-7: Strings and Text & More Printing

时间:2021-01-05 18:23:04

1、习题 6: 字符串(string) 和文本

学习目标:了解字符串的定义,学会使用复杂的字符串来建立一系列的变量。学会命名有意义的变量名

习题六中的练习代码是:

#! -*-coding=utf-8 -*-

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)

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 ? I %r"

print joke_evaluation % hilarious

w = "This is the left side of ..."
e = "a string with a right side."

print w + e

上述代码的运行结果为:

C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo6/Exer6-1.py
There are 10 types of people.
Those who know binary and those who don't.
I said: 'There are 10 types of people.'.
I also said: 'Those who know binary and those who don't.'.
Isn't that joke so funny ? I False
This is the left side of ...a string with a right side.

Process finished with exit code 0

上面的代码主要是有几个点需要注意下:

  1. 占位符的问题,%d 代表整数,%s 代表字符串,数据类型必须要匹配
  2. %r 和 %s 的区别和联系,具体详情请参考习题5
  3. “+”号带来的字符串拼接的问题,具体详情请也参考习题5
  4. 格式化输出用到的占位符还有一个重要的问题就是变量和值的一,一对应

2、加分习题:

  1. 通读程序,在每一行的上面写一行注解,给自己解释一下这一行的作用。
  2. 找到所有的”字符串包含字符串”的位置。
  3. 解释一下为什么 w 和 e 用 + 连起来就可以生成一个更长的字符串。

3、我的答案

3.1、通读程序,在每一行的上面写一行注解,给自己解释一下这一行的作用

#! -*-coding=utf-8 -*-

# 定义变量x ,并用占位符表示格式化字符串
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)

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 ? I %r"

print joke_evaluation % hilarious

w = "This is the left side of ..."
e = "a string with a right side."

# 字符串的拼接
print w + e

3.2、找到所有的”字符串包含字符串”的位置

y = "Those who know %s and those who %s." % (binary,do_not)

print "I said: %r." % x
print "I also said: '%s'." % y

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

因为这里是Python中字符串的功能,其实当使用加号运算符的时候会调用这个类的_ add() _函数,这个函数是每个类都有的,对于自定义的类,不重写这个方法,+这个运算符就没作用.

4、习题总结

习题6主要是介绍了字符串的格式化输出,% 以及 + 的运用,具体还有.format操作上一题也做了详细的阐述和练习,所以还是能看出来字符串的格式化输出还是很重要的。

5、习题 7: 更多打印

学习目标:熟练掌握字符串的格式化输出,这节主要是练习为主。

习题七中的练习代码是:

#! -*-coding=utf-8 -*-

# print的打印输出
print "Mary had a little lamb."   

# 字符串的格式化输出
print "Its fleece was white as %s." % 'snow'  

print "And everywhere that Mary went."

# 字符串的 *n 的特殊用法,相当于重复多少次
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"

# 字符串的拼接
print  end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 + end10 + end11 + end12

上述代码的执行结果为:

C:\Python27\python.exe D:/pythoncode/stupid_way_study/demo7/Exer7-1.py
Mary had a little lamb.
Its fleece was white as snow.
And everywhere that Mary went.
..........
Cheese Burger

Process finished with exit code 0

注:上述代码只是对之前几道习题的一个总结,还得课后多练习,毕竟单独靠这几道习题是远远不够的,

有一个知识点,字符串和数字的乘法 - 相当于将字符串重复了多少遍

举个栗子:

笨办法学Python - 习题6-7: Strings and Text & More Printing

还有个需要注意的地方,上面代码的最后两行,最后两个print语句中间有逗号;

# 字符串的拼接
print  end1 + end2 + end3 + end4 + end5 + end6,
print end7 + end8 + end9 + end10 + end11 + end12

注意:在Python2 中这样是可以执行的,不会报错,就相当于逗号在 print 中的作用是分隔多个待打印的值。如果去掉这个逗号,则相当于两个print 语句,相当于分开打印,会输出两行。

但是在Python3 中是不支持去掉在两个print 语句中间加逗号的,去掉就会报错。

笨办法学Python - 习题6-7: Strings and Text & More Printing

6、习题总结

习题7主要是回顾了之前学习的知识点,包括print操作,字符数串的格式化输出等。