1. 程式人生 > >str及list的索引與切片

str及list的索引與切片

#------------字串的索引與切片---------------#
s = 'ABCDLSESRE'

#索引
s1 = s[1]  #正向取,從0開始計數
print(s1)

s2 = s[-1]  #負向取,從-1開始倒數
print(s2)

# 切片, 前閉後開區間
s3 = s[0:4]
print(s3)
s4 = s[0:-1]
print(s4)

s5 = s[:]  #  全部切下來
print(s5)
s6 = s[0:]    #全取
print(s6)

#跳著取,隔固定長度擷取
s7 = s[0:5:2] # s[首,尾,步長]
print(s7)

s8 = s[4:0:-1]  #
倒著取 print(s8) s9 = s[4::-1] print(s9)