python學習筆記2-字串操作

來源:互聯網
上載者:User

標籤:index   格式   date   找不到   name   方法   div   地址   sql   

一、字串切片

#切片也能適合字串tittle=‘今 天  發 蘋 果‘for i t in enumerate(tittle):  #enumerate可以同時迴圈下標和值    print(‘%s:%s‘ %(i,t))

二、非空即真

#非空就是真print(1>2)

三、字串常用操作

#可變變數:list dic#不可變變數 tuple  str#迴圈list的時候不能進行remove刪除,會導致下標變化#例子:#不能用l2=l  這樣的記憶體位址是同一個,L變化,l2也會變l=[1,1,2,3,4,5,6,7,8]l2=l[:]for i  in l2:    if i%2!=0:        l.remove(i)print(l)name=‘   besttest  123\n‘new_name=name.strip()#預設去掉兩邊的空格和分行符號new_name2=name.lstrip()#預設去掉左邊的空格和分行符號new_name3=name.rstrip()#右邊new_name4=name.count(‘t‘)#尋找某個字元在字串裡面出現的次數new_name5=name.center(50,‘a‘)#50個a,name放中間index=name.find(‘s‘)#找字串下標,找不到返回-1,相同的返回第一個下標index2=name.index(‘s‘)#找字串下標,找不到會報錯name.upper() #把所有字母變成大寫name.lower()#把所有字母變成小寫file_name=‘a.xls‘print(file_name.endswith(‘.xls‘))#判斷一個字串是否以xls結尾f=‘{name} 歡迎光臨‘print(f.format(name=‘suki‘))#字串格式化d={‘name‘:‘suki‘,"age":‘18‘}print(d.format_map(d))#字串格式化,傳進去是一個字典sql=‘select * from user‘new_sql=sql.replace(‘select‘,‘update‘)#字串替換#最重要的方法st=‘a,b,c,d,e,f,g‘st.split(‘,‘)  #去除逗號,字串轉化成list#list變字串sl=[‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]‘,‘.join(sl)

 

python學習筆記2-字串操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.