#設定初值
data = 'In many systems, scalability becomes the primary driver as the user base grows.'
cnt = 0
#逐一取出字串內容
for d in data:
#判斷是否為a
if d=='a':
cnt+=1
#印出結果
print(f'共有{cnt}個a')
執行結果:
共有6個a
for d in data:這行程式會將data字串中的每個字逐一取出,放到變數d之中。
2. 用索引值取出字串中的每個字
程式:
#設定初值
data = 'In many systems, scalability becomes the primary driver as the user base grows.'
cnt = 0
#逐一取出字串內容
for i in range(len(data)):
#判斷是否為a
if data[i]=='a':
cnt+=1
#印出結果
print(f'共有{cnt}個a')
執行結果:
共有6個a
自主練習
句子「Learn Basic deSIgn prinCIPles of scaLABility」有多少個大寫英文字?