欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2022-07-05 15:37:49
[TOC] 1、习题 6: 字符串(string) 和文本 学习目标: 了解字符串的定义,学会使用复杂的字符串来建立一系列的变量。学会命名有意义的变量名 习题六中的练习代码是: 上述代码的运行结果为: 上面的代码主要是有几个点需要注意下: 1. 占位符的问题,%d 代表整数,%s 代表字符串,数据类 ......

目录

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 的区别和联系,具体详情请参
  3. “+”号带来的字符串拼接的问题,具体详情请也参考
  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操作,字符数串的格式化输出等。