笨方法學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 水平製表符
相關文章
相關標籤/搜索