
- replace() 取代string 中的所選字
- split () 創造一個list 把string 字串分開
- list() create a list character by charterer
- format variable.format() → 代入變數內容
- f-string 將變數設定好 再input 到 大括號設定的variable{}
# replace
name = "Allen Iverson"
print(name.replace("Allen", "Alan"))
-output-------------------------------------
Alan Iverson
# split / list
sentence = " I am fine today "
print(sentence.split()) # split setence
print(list(sentence)) # list 個別字元
output---------------------------------------------
['I', 'am', 'fine', 'today']
[' ', 'I', ' ', 'a', 'm', ' ', 'f', 'i', 'n', 'e', ' ', 't', 'o', 'd', 'a', 'y', ' ']
# 資料類型 (string, tuple, list, dict, number, boolean)
# format fstring
# fstring python 3.6
sentence = "I have {} today !".format("bad mood")
print(sentence)
print("{}, {}, {}".format("A", "20", "I am fine"))
print("{0}, {2}, {1}".format("A", "20", "I am fine"))
print("{0}, {0}, {0}".format("A", "20", "I am fine"))
print("{name}, {location}, {sport}".format(
name="albert", sport="jogging", location="Taoyaun"))
output-------------------------------------------------------
I have bad mood today !
A, 20, I am fine
A, I am fine, 20
A, A, A
albert, Taoyaun, jogging
# fstring (format 簡化版)
myname = "Albert"
sport = "jogging"
print(f"{myname} love {sport} !")
output-------------------------------------------
Albert love jogging !
Python的小括弧,中括號,大括號的區別