字串去除空白 ,strip() , 包含空格,tab鍵, 換行符號
>>> name = " Frank " >>> name.strip() 'Frank'
字串的分割, split("分隔符號"),分組後成為一個列表
>>> name = "Apple, banbana, orice" >>> name.split(",") ['Apple', ' banbana', ' orice'] >>>
字串的合併join("連接符")
>>> name = ['Frank', 'Marlon', 'Lee'] >>> "|".join(name) 'Frank|Marlon|Lee'
判斷空格是否子字串中「in」
>>> name = "Frank Bain" >>> '' in name True >>> "L" in name False >>>
兩種字串格式化列印形式format
# 第一种表示方法 >>> msg = "Hello {name}, your age is {age}" >>> msg.format(name="Frank", age=24) 'Hello Frank, your age is 24' >>> # 第二种表示方法 ,注意index 从0 开始 >>> msg = "Hello{0}, your age is {1}, sex is {2}" >>> msg.format("Frank", 24, "man") 'HelloFrank, your age is 24, sex is man' >>>
字串的判斷
>>> name = "Frank" >>> name.upper() 'FRANK' >>> name.lower() 'frank' >>> name.startswith("F") True >>> name.endswith("k") True >>> name.endswith("a") False