习题6:字符串(string)和文本
x = "There are %d types of people." % 10
binary = "binary"
doNot = "don't"
y = "Those who know %s and those who %s." % (binary, doNot)
print(x)
print(y)
print("I said: %r." % x)
print("I also said: '%s'." % y)
hilarious = False
jokeEvaluation = "Isn't that joke so funny?! %r"
print(jokeEvaluation % hilarious)
w = "This is the left side of..."
e = "a string with a right side."
print(w + e)
特意去百度了一下,%s和%r的区别,%s——str出来的值是给人看的字符串,%r——repr出来的值是给机器看的,括号中的任何内容出来后都是在它之上再加上一层引号。其实我看的也是有的晕乎,我想着以后要用也就用%s就行了,干啥用什么%r。
1. 通读程序,在每一行的上面写一行注解,给自己解释一下这一行的作用。
2. 找到所有的”字符串包含字符串”的位置,总共有四个位置。
3. 你确定只有四个位置吗?你怎么知道的?没准我在骗你呢。
4. 解释一下为什么 w 和 e 用 + 连起来就可以生成一个更长的字符串。
+为连接符,拼接的意思。
习题7:更多打印
print("Mary had a little lamb.")
print("Its fleece was white as %s." % 'snow')
print("And everywhere that Mary went.")
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'
# watch that comma at the end. try removing it to see what happens
print(end1 + end2 + end3 + end4 + end5 + end6, end7 + end8 + end9 + end10 + end11 + end12)
1. 逆向阅读,在每一行的上面加一行注解。
重复打印某段字符串,可以直接使用*
2. 倒着朗读出来,找出自己的错误。
3. 从现在开始,把你的错误记录下来,写在一张纸上。
4. 在开始下一节习题时,阅读一遍你记录下来的错误,并且尽量避免在下个练习中再犯同样的错误。
5. 记住,每个人都会犯错误。程序员和魔术师一样,他们希望大家认为他们从不犯错,不过这只是表象而已,他们每时每刻都在犯错。
print_r('点个赞吧');
var_dump('点个赞吧');
NSLog(@"点个赞吧!")
System.out.println("点个赞吧!");
console.log("点个赞吧!");
print("点个赞吧!");
printf("点个赞吧!\n");
cout << "点个赞吧!" << endl;
Console.WriteLine("点个赞吧!");
fmt.Println("点个赞吧!")
Response.Write("点个赞吧");
alert(’点个赞吧’)