# String
字串
---
## 其實你看過囉
- print("Helloworld")
- "Helloworld"
----
## What's string?
- " " 跟 ' ' 包起來的東西
- e.g. "123", 'hi'
----
## 隨堂練習-1
以下何者是string呢?
- "123"
- \"123 " "
- "123'123"'
- '213213'"'
----
## Solution
- "123" 正解
- "123""
- 最後一個"沒有對到
- "123'123"'
- 最後一個'沒有對到
- '213213'"'
- "跟'都沒對到
----
## 日常生活中的string
- 密碼學(凱薩加密)
- Tqspvut -> Sprouts
- 資料格式轉換
- "10 : 10 : 10" -> "10h 10m 10s"
---
## String vs list
----
## 共通點-1
- len
- len("123") => 3
- len([1,2,3]) => 3
- index
- "123"[1] => "2"
- [1,2,3][1] => 2
- in
- "123" in "123456" => True
- 123 in [123,234,345] => True
----
## 共通點-2 : Traverse
```python=
for i in "123":
print(i)
for i in [1,2,3]:
print(i)
```
----
## 非共通點 : Immutable
試試看以下的Code
```python=
a = [1,2,3]
a[2] = 1
b = "123"
b[2] = "1"
```
----
## TypeError
- 'str' object does not support item assignment
- string裡的元件不能被更換
![](https://i.imgur.com/7DwdSrp.png)
---
## 字串-四天王
- replace
- split
- find
- join
----
## [字串變數].replace()
- string.replace(str1,str2)
- 將string裡的str1都換成str2
----
# 牛刀小試
```
"112312312323".replace("123","")
```
1. "123"
2. ""
----
## Example
```python=
a = "rilakeeeeeeee"
print(a.replace("e",""))
# rilak
```
----
## [字串變數].split()
- string.split(str,n)
- 從string裡所有str的地方斷開,最多斷n個,也可以不要寫n
----
## Example
```python=
a = "rilak,achin,pie,arbuz"
a.split(",")
# ["rilak","achin","pie","arbuz"]
a.split(",",2)
# ["rilak","achin","pie,arbuz"]
```
----
## [字串變數].find()
- string.find(str,start)
- 從第start個字開始找str,回傳其index,start可不填
----
## Example
```python=
a = "rilak achine pie arbuz"
a.find("e ")
# 11
a.find("e ",12) #故意跳過第一個
# 15
```
----
## 練習
- 3042 : 傳奇野豬騎士陳刀III
- 3045 : 野豬騎士史記 I
----
## [字串變數].join()
- string.join(list)
- 以string為連接字串,將list裡的東西串在一起
- Warning : list裡都要是字串
----
## Example
```python=
a = ["hortune","rilak","sky","buzz"]
", ".join(a)
# 'hortune, rilak, sky, buzz'
```
---
# Slice
----
## 字元的list
string其實某種意義上是字元的list,所以list的slicing操作都可以使用
----
### "字串變數"[start : end : change]
- start : index 初始值
- end : index 終止值
- 開區間
- change : index 變化量
----
## Example
```python=
a = "Hello world"
a[:a.find(" ")]
# "Hello"
a[a.find(" ")+1:]
# "world"
a[::-1] # 翻轉
# "dlrow olleH"
```
---
## String的雜function
- upper
- lower
- isalpha
----
## Example
```python=
"hello".upper()
# HELLO
"HELLO".lower()
# hello
"123".isalpha()
# False
"abc".isalpha()
# True
```
---
## String 四則運算
- 加法
- 乘法
- 相等
- 不相等
----
## 加法 (concat)
```python=
a = "Hi "
b = "Sprouts"
a + b
# 'Hi Sprouts'
```
----
## 乘法 (duplicate)
```python=
"MuQ" * 10
# "MuQMuQMuQMuQMuQMuQMuQMuQMuQMuQ"
```
----
## 相等 (equal)
```python=
"123" == "321"
# False
"1" == "1"
# True
```
----
## 不相等
```python=
"123" != "321"
# True
"1" != "1"
# False
```
----
## 大於小於??
```python=
10123123 > 11
# True
"10123123" > "11"
# False
```
----
## 字典序
- python的字串比較是一個字一個字依照ascii下去比,"1"跟"1"一樣大,於是比下一個字,"0"小於"1",所以"11"比較大。
- 詳細可以去看Ascii跟ord和chr兩個function
----
## 隨堂練習-2
- 3046 : 西瓜的名字
---
# Bonus!!!
----
## Format String
----
## 情境
- 資料格式轉換
- "10 : 10 : 10" -> "10h 10m 10s"
----
## You write?
```python=
string = "10 : 10 : 10"
numbers = string.split(" : ")
print(numbers[0]+"h ",numbers[1]+"m",numbers[2]+"s")
```
----
## Cons
- 醜
- 麻煩
----
## Elegant Way
```python=
string = "10 : 10 : 10"
"{}h {}m {}s".format(*string.split(" : "))
```
----
# WTF
----
## 關鍵字-1
format
----
## 關鍵字-2
`{}`
----
## 奇怪點
- *string.split(":")
- 先不管他
----
# 進入正題
----
## Example
把後面塞進去
```python=
"{}".format(1)
# 1
"{} {}".format(1,2)
# 1 2
```
----
## 回到剛剛
```python=
string = "10 : 10 : 10"
"{}h {}m {}s".format(*string.split(" : "))
```
### 想成
```python=
string = "10 : 10 : 10"
"{}h {}m {}s".format(10,10,10)
```
----
## 小知識 : *
- \* list可以用來展開list,然後當成function的input。
----
## Advance
```python
"{} {}".format(1,2)
# 1 2
"{0} {1}".format(1,2)
# 1 2
"{1} {0}".format(1,2)
# 2 1
```
----
## More
[pyformat](https://pyformat.info/)
---
## Things Not Mentioned
- Ascii
- 跳脫字元
----
## Homework
- [通靈之塔-靈的力量](https://neoj.sprout.tw/problem/3056/)
{"metaMigratedAt":"2023-06-14T15:41:06.790Z","metaMigratedFrom":"YAML","title":"String","breaks":true,"slideOptions":"{\"theme\":\"solarized\",\"transition\":\"fade\"}","contributors":"[]"}