python基礎(四)字串處理

來源:互聯網
上載者:User

標籤:lin   參數   blog   windows   字母   inux   mis   根據   dex   

字串處理
msg = ‘my name is sylar‘
capitalize方法,將字串的首字母大寫
print ‘capitalize方法:‘, msg.capitalize()
swapcase方法用於對字串的大小寫字母進行轉換。upper
print ‘swapcase方法:‘, msg.swapcase()print ‘upper方法:‘,msg.upper()msq_lower = msg.upper()print ‘lower方法:‘,msq_lower, msq_lower.lower()
所有單詞都是以大寫開始,其餘字母均為小寫(見 istitle())
print ‘title方法:‘,msg.title()
count方法,查詢子串出現的次數
print msg.count(‘name‘)print ‘count方法:‘, msg.count(‘a‘)
center:用指定的字元,將字串填充到指定的長度左
print ‘center方法:‘, msg.center(40,‘x‘)
ljust將原字串靠左對齊,並使用空格填充至指定長度的新字串。如果指定的長度小於原字串的長度則返回原字元.填充字元,預設為空白格。
print ‘ljust方法:‘, msg.ljust(20,‘x‘)
rjust將原字串靠右對齊,並使用空格填充至長度 width 的新字串。如果指定的長度小於字串的長度則返回原字串。
print ‘rjust方法:‘, msg.rjust(20,‘x‘)
字元編碼 #decode,encode
msg2 = ‘我是sylar‘msg3 = u‘我是sylar‘print ‘msg2:‘,msg2,type(msg2),‘\tmsg3:‘, msg3, type(msg3)msg2 = msg2.decode(‘utf-8‘)msg3 = msg3.encode(‘utf8‘)print ‘msg2.decode:‘,msg2, type(msg2), ‘\tmsg3.decode:‘,msg3,type(msg3)
endswith,startswith 尋找字串的開始和結束位置的字串
print msg.endswith(‘sylar‘)print msg.endswith(‘tom‘)print msg.startswith(‘my‘)print msg.startswith(‘MY‘)
expandtabs 將定位字元TAB替換為空白格 windows這裡不太明顯.Linux下面是改變定位字元的長度
msg_expandtabs = ‘my name is\tsylar‘print ‘expandtabs方法:‘,msg.expandtabs()
fing,index方法 #index檢查是否包含一個子串位置碼和find方法一樣,但是會報告一個錯誤返回!,find方法不會返回錯誤返回-1
print ‘index方法:‘,msg.index(‘y‘)
print ‘index方法返回錯誤:‘,msg.index(‘sylra‘)
print ‘find方法:‘,msg.find(‘y‘)print ‘find方法沒找到:‘,msg.find(‘tom‘)
rfind,rindex返回字串最後一次出現的位置 find,index是第一次出現的位置
print ‘rfind方法:‘,msg.rfind(‘y‘)print ‘rindex方法:‘,msg.rindex(‘y‘)
format 格式化輸出字串
msg.format()age = ‘88‘name = ‘sylar‘print(‘{0} is {1} years old. ‘.format(name, age)) #輸出參數print(‘{0} is a girl. ‘.format(name))
字串類型判斷
age_num = age.isalnum() #所有字元都是數字或者字母age_dig = age.isdigit() #所有字元都是數字age_alp = age.isalpha() #所有字元都是字母age_low = age.islower() #所有字元都是小寫age_spc = age.isspace() #所有字元都是空白字元age_tit = age.istitle() #所有單詞都是首字母大寫,像標題age_upp = age.isupper() #所有字元都是大寫print ‘字串類型判斷:‘,age_num,age_dig,age_alp,age_low,age_spc,age_tit,age_upp
lstrip方法用於 截掉字串左邊的空格或指定字元。rstrip 刪除 string 字串末尾的指定字元,strip方法用於移除字串頭尾指定的字元(預設為空白格)
msg_strip = ‘xxxkslfsjflsjxxx‘print ‘lstrip方法:‘, msg_strip.lstrip(‘x‘)print ‘rstrip方法:‘,msg_strip.rstrip(‘x‘)print ‘strip方法:‘,msg_strip.strip(‘x‘)
截取指定長度的字串,原字串靠右對齊,前面填充0
print ‘zfill方法:‘,msg.zfill(10)
replace方法 替換字串中的字串,如果指定第三個參數count,則替換不超過 count 次
print ‘replace方法‘,msg.replace(‘is‘,‘\033[31;1mis\033[0m‘)print ‘replace方法‘,msg.replace(‘is‘,‘\033[31;1m%s\033[0m‘%name)
join將序列中的元素以指定的字元串連產生一個新的字串
join_list = [‘1‘,‘2‘,‘3‘,‘4‘]msg_join =‘‘.join(join_list)print ‘join方法:‘, msg_join ,type(msg_join)
split,rsplit 將字串格式化為列表,不指定切割字元預設為空白格,還可以指定切割次數,split 從左往右,rsplit從右向左(預設為空白格)
msg_spt = msg.split(‘y‘,1)print msg_spt,type(msg_spt),msg_spt[0] #當列表內有中文是,直接列印列表返回Unicode編碼,取值時則返回中文msg_spt = msg.rsplit(‘y‘,1)print msg_spt,type(msg_spt),msg_spt[0]
partition 方法 將字串劃為元祖 根據指定的分隔字元將字串進行分割。
print ‘partition方法:‘, msg.partition(‘y‘), type(msg.partition(‘y‘))print ‘rpartition方法:‘, msg.rpartition(‘y‘),type(msg.rpartition(‘y‘))
splitlines按照行分隔,返回一個包含各行作為元素的列表,如果 num 指定則僅切片 num 個行
msg_splitlines=‘1\n2\n3\n4‘print ‘splitlines‘,msg_splitlines.splitlines()
translate()根據參數table給出的表(包含 256 個字元)轉換字串的字元, 要過濾掉的字元放到 del 參數中。

python基礎(四)字串處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.