写给某人:有一位同学天天在我耳边念叨Python语言如何如何好(请恕我词穷不知如何复述那个如何如何到底是有多如何如何),所以下定决心要从头开始学习Python了。写代码一直是我的软肋,希望这次能坚持学习,边学边记。
跟着笨方法学Python个教程学习,边学变做习题边记录。
新手学python-笨方法学python-ex6-字符串(string)和文本 - 学习记录
本小节学习内容:
虽然你已经在程序中写过字符串了,你还没学过它们的用处。在这章习题中我们将使用复杂的字符串来建立一系列的变量,从中你将学到它们的用途。首先我们解释一下字符串是什么 东西。
字符串通常是指你想要展示给别人的、或者是你想要从程序里“导出”的一小段字符。Python 可以通过文本里的双引号 " 或者单引号 ' 识别出字符串来。这在你以前的 print 练习中你已经见过很多次了。如果你把单引号或者双引号括起来的文本放到 print 后面,它们就会被 python 打印出来。
字符串可以包含格式化字符 %s,这个你之前也见过的。你只要将格式化的变量放到字符串中,再紧跟着一个百分号 % (percent),再紧跟着变量名即可。唯一要注意的地方,是如果你想要在字符串中通过格式化字符放入多个变量的时候,你需要将变量放到 ( ) 圆括号(parenthesis)中,而且变量之间用 , 逗号(comma)隔开。就像你逛商店说“我要买牛奶、面包、鸡蛋、八宝粥”一样,只不过程序员说的是”(milk, eggs, bread, soup)”。
我们将键入大量的字符串、变量、和格式化字符,并且将它们打印出来。我们还将练习使用简写的变量名。程序员喜欢使用恼人的难度的简写来节约打字时间,所以我们现在就提早学会这个,这样你就能读懂并且写出这些东西了。
跟着课程学习:
[root@lc test]# vi ex6.py
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 said: '%s'." % y
hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"
print joke_evaluation % hilarious
w = "This is the left side of..."
e = "a string with a right side."
print w + e
"ex6.py" 21L, 392C written
[root@lc test]#
执行该程序:
[root@lc test]# python ex6.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 said: 'Those who know binary and those who don't.'.
Isn't that joke so funny?! False
This is the left side of...a string with a right side.
[root@lc test]#
加分习题:
1.通读程序,在每一行的上面写一行注解,给自己解释一下这一行的作用。
[root@lc test]# vi ex6-1.py
#define x as "There are %d types of peole." where the %d is 10
x = "There are %d types of people." % 10
#define binary as binary
binary = "binary"
#define do_not as don't
do_not = "don't"
#define y as "Those who know %s and those who %s." where the 1st %s is the content of variable binary and the 2nd %s is the content
variable do_not
y = "Those who know %s and those who %s." % (binary, do_not)
#print x
print x
#print y
print y
#print "I said: %r." where the %r is all the content the variable x has,include all""
print "I said: %r." % x
#print "I said: '%s'." where the %s is the content of the variable y
print "I said: '%s'." % y
#define hilarious as False
hilarious = False
#define joke_evaluation as "Isn't that joke so funny?! %r"
joke_evaluation = "Isn't that joke so funny?! %r"
#print Isn't that joke so funny?! %r where %r is the content of variable hilariour
print joke_evaluation % hilarious
#define w as a string
w = "This is the left side of..."
#define e as a string
e = "a string with a right side."
#print string a and string b
print w + e
"ex6-1.py" 29L, 1061C written
[root@lc test]#
执行该程序:
[root@lc test]# python ex6-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 said: 'Those who know binary and those who don't.'.
Isn't that joke so funny?! False
This is the left side of...a string with a right side.
[root@lc test]#
2.找到所有的”字符串包含字符串”的位置,总共有四个位置。
1)y = "Those who know %s and those who %s." % (binary, do_not) - 这里有两个
2)print "I said: %r." % x 这里第三个
3)print "I said: '%s'." % y 这里第四个
4)print joke_evaluation % hilarious 这里第五个
3.你确定只有四个位置吗?你怎么知道的?没准我在骗你呢。
找出来5个
4.解释一下为什么 w 和 e 用 + 连起来就可以生成一个更长的字符串。
Python的print + 可以连接字符串, *数字可以循环重复字符串,例如:
>>> a = "test "
>>> b = "this is for test"
>>> print a + b
test this is for test
>>> print a *3
test test test
>>> print b * 4
this is for testthis is for testthis is for testthis is for test
>>> print a * 3 + b * 4
test test test this is for testthis is for testthis is for testthis is for test
>>>
至此,这一小节的学习结束了。