笨方法学Python,Lesson6,7,8,9,10

Exercise 6
python

代码shell

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

print joke_evaluation % hilarious

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

print w + e

输出ide

Notes:this

①格式化字符的区别,%r主要用于debug,%s字符串,%d整型lua

②字符串之间能够用+相连,组成新的长字符串
debug

>>> 'abc' + 'defg'
'abcdefg'

③True、False均是python关键字,是布尔值code

Exercise 7orm

代码three

print "Marry had a little lamb."
print "Its fleece was white as %s." % 'snow'
print "And everywhere that Marry went."
print "." * 10 

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

输出ip

Notes:

无新内容

Exercise 8

代码

# -*- coding:utf-8 -*-
formatter = "%r %r %r %r"

print formatter % (1,2,3,4)
print formatter % ("one","two","three","four")
print formatter % (True,False,False,True)
print formatter % (formatter,formatter,formatter,formatter)
print formatter % (
    "I had this thing.",
    "That you could type up right.",
    "But it didn't sing.",
    "So I said goodnight."
)

输出

Exercise 9

代码

# Here's some new strange stuff, remember type it exactly.

days = "Mon Tue Wed Thu Sat Sun"
months = "Jan\nFeb\nMar\nApr\nMay\nJun\nJul\nAug"

print "Here are the days:", days 
print "Here are the months:", months 

print """
There's something going on here.
With the three double-quotes.
We'll be able to type as much as we like.
Even 4 lines if we want, or 5, or 6.
"""

输出

Notes:

① \n在字符串中直接换行

②三个双引号或单引号内的字符串,能够直接输入多行并输出多行

Exercise 10

代码

tebby_cat = "\tI'm tabbed in."
persian_cat = "I'm split\non a line."
backslach_cat = "I'm \\ a \\ cat."

fat_cat = """
I'll do a list:
\t* Cat food
\t* Fishies
\t* Catnip\n\t* Grass
"""

print tebby_cat
print persian_cat
print backslach_cat
print fat_cat

输出

Notes:

①\是转义符  后面跟不一样的转义字符造成转义序列能够在字符串中实现不一样的效果

             转义符
                                   功能
\\ 反斜杠
\' 单引号
\'' 双引号
\a
ASCII Bell 响铃符
\b 退格符
\f 进纸符
\n 换行符
\r ASCII
回车符
\v 垂直制表符
\t 水平制表符
相关文章
相关标签/搜索