# (1) 逐一取出字串內容

### 1. 使用for...in...取出字串中的每個字&#x20;

{% code title="程式：" %}

```python
#設定初值
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')
```

{% endcode %}

{% code title="執行結果：" %}

```python
共有6個a
```

{% endcode %}

<figure><img src="https://1148930333-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJvF1wk3Ixe9l9GXQ70%2Fuploads%2Fhnr4eWfFXjrfvo1gA7eP%2FScreen%20Shot%202022-9-15%20at%20%E4%B8%8B%E5%8D%88%2008.32.png?alt=media&#x26;token=4cc863ff-75da-496b-9431-1394a1adc844" alt=""><figcaption></figcaption></figure>

`for d in data：`這行程式會將`data字串`中的每個字逐一取出，放到`變數d`之中。

### 2. 用索引值取出字串中的每個字

{% code title="程式：" %}

```python
#設定初值
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')
```

{% endcode %}

{% code title="執行結果：" %}

```python
共有6個a
```

{% endcode %}

<figure><img src="https://1148930333-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MJvF1wk3Ixe9l9GXQ70%2Fuploads%2FPVoJip6UJQnFLItGmMi7%2FScreen%20Shot%202022-9-15%20at%20%E4%B8%8B%E5%8D%88%2008.40.png?alt=media&#x26;token=6387b2bf-904c-4de0-8189-2982807d570f" alt=""><figcaption></figcaption></figure>

### 自主練習

1. 句子「Learn Basic deSIgn prinCIPles of scaLABility」有多少個大寫英文字？
