2008年8月7日 星期四

python 的字串切割

python的字串切割

該怎麼說起呢?

例如:

world="abcde" 這個字串

看得懂吧?

先說喔 world中的第一個字

它的index是0喔

例如: world[0]

>>>'a'


我們可以用 world[0:]

她就print 出

>>>'abcde'

懂吧?

看不懂?!

再試幾個例子

world[2:4]

就print 出

>>>'cd'


不過 我覺得比較好玩的是

他的index可以用負數的

如果是負的 就是從字串的最後那個字元開始算起喔

例如:world[-1]

就print出
>>>'e'


我們再玩幾個例子吧

world[0:-1]

>>>'abcd'


world[-2]

>>>'d'

咦?

world[0]

>>>'a'

跟 world[-5]
>>>'a'

是一樣的結果 ~~

這下子你知道了吧^^

沒有留言:

Class has no initializers in swift

Class has no initializers in swift I have met this situation. Found a solution here It says if you have the code like bellow var se...