s = "make me great again" # 字串
li = ['make', 'me', 'great', 'again'] # 串列
tu = ('make', 'me', 'great', 'again') # 元組
print(s[2], s[4]) # k
print(li[2], li[0]) # great make
print(tu[2], tu[1]) # great me
s = "Angels Like You"
# 從編號2號的格子到編號4號的格子
print(s[2:5]) # gel
# 從編號0號的格子到編號14號的格子,取樣間隔為2
print(s[0:15:2]) # Agl ieYu
# 從編號10號的格子到編號1號的格子,取樣間隔為-2
print(s[10:0:-2]) # ei lg
tu = ('AE', 'CN', 'FR')
tu[1] = 'GB' # TypeError: 'tuple' object does not support item assignment
del thistuple[1] # TypeError: 'tuple' object doesn't support item deletiondel thistuple[1] # TypeError: 'tuple' object doesn't support item deletion
間接改動元組內容的值(先轉為list變動完後再轉回tuple)
tu = ('AE', 'CN', 'FR')
y = list(tu) # 先轉為list
y[1] = "panda"
tu = tuple(y) # 再轉回tuple
print(tu) # ('AE', 'panda', 'FR')
H. 陣列中的子陣列
使用index()
字串
s = 'Those who do nothing but allow themselves to dwell in despair have no right to complain about life.'
print(s.index('dwell')) # 45 (找出串列中第一個相符合的子字串所在的索引值)
print(s.index('to', 45)) # 76 (找出索引值45之後第一個相符合的子字串所在的索引值)
print(s.index('gorgeous')) # ValueError: substring not found
串列
fruits = ['orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana']
print(fruits.index('banana')) # 3 (找出串列中第一個相符合的值所在的索引值)
print(fruits.index('banana', 4)) # 6 (找出索引值4之後第一個相符合的值所在的索引值)
print(fruits.index('guava')) # ValueError: 'guava' is not in list
元組
fruits = ('orange', 'apple', 'pear', 'banana', 'kiwi', 'apple', 'banana')
print(fruits.index('banana')) # 3
print(fruits.index('banana', 4)) # 6
print(fruits.index('guava')) # ValueError: tuple.index(x): x not in tuple
s = 'Those who do nothing but allow themselves to dwell in despair have no right to complain about life.'
print(s.find('dwell')) # 45 (找出串列中第一個相符合的子字串所在的索引值)
print(s.find('to', 45)) # 76 (找出索引值45之後第一個相符合的子字串所在的索引值)
print(s.find('gorgeous')) # -1
判斷某子陣列是否有在某陣列中
使用 in 或 not in
字串
txt = "A beautiful woman wears a smile on her face, carries confidence on her shoulder, keeps courage in her heart, and holds destiny in her hand."
conclusion1 = "shoulder" in txt # 判斷 shoulder 有無在 txt 字串中
conclusion2 = "beautiful" not in txt # 判斷 beautiful 有無在 txt 字串中
print(conclusion1, conclusion2) # True False
串列
thislist = ['USA', 'DE', 'TW']
if 'TW' in thislist:
print('Yes') # Yes
if 'CN' in thislist:
print('Yes') # (不會印出)
元組
thislist = ('USA', 'DE', 'TW')
if 'TW' in thislist:
print('Yes') # Yes
if 'CN' in thislist:
print('Yes') # (不會印出)
I. 陣列搭配迴圈
字串
s = "世界が終るまでは"
for x in range(len(s)):
print(s[x], end=" ") # 世 界 が 終 る ま で は
s = "世界が終るまでは"
for x in s: # x變數會從字串陣列中的第一格數值開始到最後一格
print(x, end=" ") # 世 界 が 終 る ま で は
串列
li = ['USA', 'DE', 'TW']
for x in range(len(li)):
print(li[x] , end=' ') # USA DE TW
li = ['USA', 'DE', 'TW']
for x in li:
print(x , end=' ') # USA DE TW
li = [1, 2, 3, 4, 5]
l = [element - 1 for element in li] # 將串列內所有值都-1
print(l) # [0, 1, 2, 3, 4]
元組
tu = ('USA', 'DE', 'TW')
for x in tu:
print(x , end=' ') # USA DE TW
tu = (1, 2, 3, 4, 5)
t = [element - 1 for element in tu]
print(t) # [0, 1, 2, 3, 4]
J. 陣列變字串
使用join()
字串每隔間插入新字串
s = "有拜有保庇"
print('--'.join(s)) # 有--拜--有--保--庇
串列每隔間插入新字串
li = ['有', '拜', '有', '保', '庇']
print(''.join(li)) # 有拜有保庇
li = ['有', '拜', '有', '保', '庇']
print('--'.join(li)) # 有--拜--有--保--庇
元組每隔間插入新字串
li = ('有', '拜', '有', '保', '庇')
print('--'.join(li)) # 有--拜--有--保--庇
contry = ('CA', 'USA', 'TW', 'DE', 'AE', 'I')
print(max(contry, key=len)) # USA
print(min(contry, key=len)) # I
II. 字串(str)專屬用法
A. 英文字母大小寫轉換
使用lower()及upper()
a = "Courage is not a proof of Strength, but an act of Persistence."
print(a.lower()) # courage is not a proof of strength, but an act of persistence.
print(a.upper()) # COURAGE IS NOT A PROOF OF STRENGTH, BUT AN ACT OF PERSISTENCE.
B. 字串與變數或數字串接
name = "Lance"
age = 18
print('the age of {} is {}'.format(name, age)) # the age of Lance is 18
name = "Lance"
age = 18
print(f'the age of {name} is {age}') # the age of Lance is 18
print(f'the age of{name}is{age}') # the age ofLanceis18
print("the age of {} is {}".format("Lance", 18)) # the age of Lance is 18
print(f'the age of {"Lance"} is {18}') # the age of Lance is 18
# 創造一個2x3的2維陣列
twoDarray = [[1, 2, 3],
[4, 5, 6]]
print(twoDarray[1][0]) # 4
print(twoDarray[0][3]) # IndexError: list index out of range
print(twoDarray[1][1]) # 5