python字串的常用方法

2021-10-16 22:44:48 字數 770 閱讀 2988

##python字串的常用方法

len("hello"): 返回字串的長度

str.capitalize() : 將原來的字串的首字母大寫,並返回,返回值為str型別

str.lower(): 將字串所有字母小寫,並返回,返回值為str型別

str.upper(): 將字串所有字母大寫,並返回,返回值為str型別

str.count('a'): 判斷字串存在幾個字元a, 返回值為int型別

str.find('a'): 判斷字元a在字串中的位置,返回字元a的索引

str.split('.'): 將字串中的 . 分隔開 ,返回乙個列表物件

str.strip() 該方法用於移除字串頭尾指定的字元(預設為空格)(只能移除頭尾的,不能移除中間的字元)。

「--".join(a) 將分隔的字元用--連線起來

str.replace("aa","bb") 將字串中所有的aa字元替換為bb字元

str.startswith("aa") 判斷字串是否以aa開頭,返回true或者false

str.format() 格式化字串

str.encode("utf-8"): 對字串進行utf-8編碼

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...