python 常用的字串方法

2022-08-04 22:27:18 字數 682 閱讀 3596

st = '   hello kitty   '

str = 'hello '

#print(st.format(name='fadfa'))

#常用的字串方法

print(st.count)

print(st.count('l'))

print(st.center(50,'#'))

print(st.startswith('h'))

print(st.find('t'))

print(str.format(name='ligq',age=25))

print(st.lower()) #將小寫字母轉大寫,不對源字串操作

print(st)

print(st.upper())

print(st)

print(st.strip()) #去掉字串前後的空格、換行符、製表符

print(st.replace('tty','yyt'))

sstr = 'hello hello hello kitty'

print(sstr.replace('ll','gg',1)) #替換字串內指定的字元。後面的引數指定從左到右替換幾個。

print(sstr.split(' ',2)) #以指定的字元分割字串,得到乙個列表。後面的引數指定從左到右分割幾次。

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