天天看點

小白學習Python筆記(2)

二、元組「tuple」

tuple與list相似,但是它是"當機的",不可修改、插入、删除。其他皆相同。

元組形式是( )圓括号, 但引用其元素形式仍是[ ]

eg:

t1 = (1,2,3,4)
print(t1[0])   #1
print(t1[::—1])   #倒序輸出
           
print(t1[1 in t1])  #輸出:True
           
檢查 1 是否在 t1 中
傳回某個元素對應的索引值

三、字元串「string」

單/雙引号括起

取字元串中的字元類似取清單中的元素

(一)索引通路

str1 = ‘abcd’
print(str1[0])   #輸出a
           

(二)切片

print(str1[:2]) 
#ab
           

(三)添加

str1 =  str1 +'efg'*2
print(str1)           #'abcdefgefg'
           

(四)格式化字元串

print('{} is  an excellent  {}.' .fomat('he', 'xiaobai'))
#he is an excellent xiaoba.
           

(五)顯示指定字段

print('{3} is my {0},{2} is {1}.'.fomat('duty', 'love','study','learning'))
#learning is my duty, sudy is love.
           
多行字元串用三個單引号标記
long_str = '''i am a xiaobai,
 but i like study,
 study make me strong.'''
 print (long_str)