多行語句
Python 通常是一行寫完一條語句,但如果語句很長,我們可以使用反斜槓(\)來實現多行語句,例如:
total = item_one + \
item_two + \
item_three
在 [], {}, 或 () 中的多行語句,不需要使用反斜槓(\),例如:
total = ['item_one', 'item_two', 'item_three',
'item_four', 'item_five']
- 反斜槓可以用來轉義,使用r可以讓反斜槓不發生轉義。。 如 r"this is a line with \n" 則\n會顯示,並不是換行。
- 字符串可以用 + 運算符連接在一起,用 * 運算符重複。
- Python 中的字符串有兩種索引方式,從左往右以 0 開始,從右往左以 -1 開始。