龙腾海啸txt,别杀那个胖家伙,四川银杏学院
目录
学习目标:了解字符串的定义,学会使用复杂的字符串来建立一系列的变量。学会命名有意义的变量名
习题六中的练习代码是:
#! -*-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
上面的代码主要是有几个点需要注意下:
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() _函数,这个函数是每个类都有的,对于自定义的类,不重写这个方法,+这个运算符就没作用.
习题6主要是介绍了字符串的格式化输出,% 以及 + 的运用,具体还有.format操作上一题也做了详细的阐述和练习,所以还是能看出来字符串的格式化输出还是很重要的。
学习目标:熟练掌握字符串的格式化输出,这节主要是练习为主。
习题七中的练习代码是:
#! -*-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
注:上述代码只是对之前几道习题的一个总结,还得课后多练习,毕竟单独靠这几道习题是远远不够的,
有一个知识点,字符串和数字的乘法 - 相当于将字符串重复了多少遍
举个栗子:
还有个需要注意的地方,上面代码的最后两行,最后两个print语句中间有逗号;
# 字符串的拼接 print end1 + end2 + end3 + end4 + end5 + end6, print end7 + end8 + end9 + end10 + end11 + end12
注意:在python2 中这样是可以执行的,不会报错,就相当于逗号在 print 中的作用是分隔多个待打印的值。如果去掉这个逗号,则相当于两个print 语句,相当于分开打印,会输出两行。
但是在python3 中是不支持去掉在两个print 语句中间加逗号的,去掉就会报错。
习题7主要是回顾了之前学习的知识点,包括print操作,字符数串的格式化输出等。
如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复
Python爬虫:Request Payload和Form Data的简单区别说明
浅谈Python中threading join和setDaemon用法及区别说明
Python3-异步进程回调函数(callback())介绍
python继承threading.Thread实现有返回值的子类实例
Python中使用threading.Event协调线程的运行详解
网友评论