標籤:less python dex exp rip 列表 join log 個數
#字串操作 #定義字串 a = "let‘s go" #字串切片 print("helloworld"[2:]) #通過索引擷取字串中的字元,和列表切片同理 #關鍵字 in print("llo" in "helloworld") #返回TRUE或False #字串拼接 #可以用+拼接字串 #join實現拼接 a = "123" b = "456" d = "789" c=‘***‘.join([a,b,d]) #用*拼接a,b,c #返回123***456***789 #% 格式化輸出字串 print(‘there are some examples‘) print(‘%s are some examples‘ %‘there‘) #字串的內建方法 st = "hello kitty{name}" st.count("t") #統計t的個數 st.capitalize #首字母大寫 st.center(50."#") #置中 st.endswith(‘ty‘) #以某個內容結尾 st.startswith(‘he‘) #以某個內容開頭 st.expandtabs(tabsize = 20) #修改縮排 st.find(‘t‘) #尋找,返回索引值 st.format(name = "haha") #格式化輸出的另一種方式 print(st.index(‘t‘)) #返回t的索引值 print(‘My tLtle‘.lower()) #將字串裡的字母全部轉換為小寫 print(‘My tLtle‘.upper()) #將字串裡的字母全部轉換為大寫 print(‘\tMy tLtle\n‘.strip()) #將字串裡不顯示的內容去掉 print(‘My title title‘.replace(‘itle‘,‘lesson‘,1)) #替換,replace(‘被替換的值‘,‘替換後的內容‘,1(替換幾個)) print(‘My title title‘.split(‘i‘,1)) #分割,split(‘分割值‘,分割次數)
【Python】-- 字串