Python字串常用方法

2021-10-13 02:57:55 字數 358 閱讀 3858

len():字串長度

find():若不為-1,則獲取到下標索引,反之沒有拿到索引

index():若不為-1,則獲取到下標索引,反之報錯

count():統計字元在字串**現的次數

replace():替換 replace(舊字元,新字元,替換個數)

capitalize():字串第乙個字母大寫

title():字串中所有單詞的首字母大寫其餘字母小寫

startswith(obj,開始,結尾):以obj開頭是true否則false

endswith(obj):以obj結束

lower():字串中所有字母小寫

upper():字串中所有字母大寫

python 字串常用方法

python 字串的常用方法 1.len str 字串的長度 2.startswith str 檢視字串是否以str子串開頭,是返回true,否則返回false 3.index str 查詢字串中第一次出現的子串str的下標索引,如果沒找到則報錯 4.find str 查詢從第0個字元開始查詢第一次...

Python字串常用方法

count 獲取字串中某個字元的數量。str python count str.count o 2strip 刪除字串首位的空格,以及換行。str1 hello python str2 hello python str3 hello python str1.strip str2.strip str3...

python字串常用方法

string.title python title 方法返回 標題化 的字串,就是說所有單詞都是以大寫開始,其餘字母均為小寫 見 istitle string.upper python upper 方法將字串中的小寫字母轉為大寫字母。string.lower 將字串的大寫字母轉為小寫字母 strin...