Octopress的教學

記錄如何安裝octopress

Python筆記

好吧!又回來寫python了,不應該在ruby和python間晃盪,這樣永遠都學不好的。

字串

在ryby裡面是length,但是python更簡潔,只用了len,如果要計算字串的長度,就直接用len(string),第二行要定義一下

例如:

1
2
3
parrot="Norwegian Blue"    
x=len(parrot)  
print x 

而大寫和小寫則是兩種程式都一樣,使用upper和lower

另外空白要寫在字串裡面,例如"argus "s後面有space,那print”argus “+”and”就會成argus and

%的用法

1
2
3
4
string_1 = "Camelot"
string_2 = "place"

print "Let's not go to %s. 'Tis a silly %s." % (string_1, string_2)

結果就是Let’s not go to Camelot. ‘Tis a silly place.

1
2
3
4
5
6
name = raw_input("What is your name?")
quest = raw_input("What is your quest?")
color = raw_input("What is your favorite color?")

print "Ah, so your name is %s, your quest is %s, " \
"and your favorite color is %s." % (name, quest, color)

結果會一個一個問你 What is your name?….. 最簡單的也可以用

1
2
myname= 'arguskao' #重點是 一定要有'把東西包起來'
print "Myname is %s " % myname

Comments