python之字符串函數
阿新 • • 發佈:2018-07-18
none spa opened alex int 占位符 圖片 是否 gif
1. endswith() startswith()
1 # 以什麽什麽結尾 2 # 以什麽什麽開始 3 test = "alex" 4 v = test.endswith(‘ex‘) 5 v = test.startswith(‘ex‘) 6 print(v)View Code
2. expandtabs()
1 test = "1\t2345678\t9" 2 v = test.expandtabs(6) 3 print(v,len(v))View Code
3. find()
1 # 從開始往後找,找到第一個之後,獲取其未知 2 # > 或 >= 3 test = "View Codealexalex" 4 # 未找到 -1 5 v = test.find(‘e‘) 6 print(v)
4. index()
1 # index找不到,報錯 忽略 2 test = "alexalex" 3 v = test.index(‘a‘) 4 print(v)View Code
5. format() format_map()
1 # 格式化,將一個字符串中的占位符替換為指定的值 2 test = ‘i am {name}, age {a}‘ 3 print(test) 4 v = test.format(name=‘alex‘,a=19)View Code5 print(v) 6 7 test = ‘i am {0}, age {1}‘ 8 print(test) 9 v = test.format(‘alex‘,19) 10 print(v) 11 12 # 格式化,傳入的值 {"name": ‘alex‘, "a": 19} 13 test = ‘i am {name}, age {a}‘ 14 v1 = test.format(name=‘df‘,a=10) 15 print(v1) 16 v2 = test.format_map({"name": ‘alex‘, "a": 19}) 17 print(v2)
6. isalnum()
1 # 字符串中是否只包含 字母和數字 2 test = "er;" 3 v = test.isalnum() 4 print(v)View Code
python之字符串函數