python之路-06-字串操作

來源:互聯網
上載者:User

標籤:find   否則   form   auth   swap   isa   exp   print   nes   

 

 

#!Author:lanhan

name = "my  \tname is {name} and i an {year} old "

print(name.capitalize())   #首字母大寫
print(name.count("a"))    #統計a出現次數
print(name.center(50,"-"))  #字串在中間,兩邊用-補齊
print(name.endswith("han")) #判斷字串是否以 han結尾
print(name.expandtabs(tabsize=30))  #將\t轉換成多長的空格
print(name[name.find("i"):])   #尋找i及後面的字串
print(name.format(name=‘lanhan‘,year=23))
print(name.format_map( {‘name‘:‘lanhan‘,‘year‘:23}))

print(‘ab23‘.isalnum())    #是否包含阿利伯數字
print(‘abA‘.isalpha())     #如果字串至少有一個字元並且所有字元都是字母則返回 True,否則返回 False
print(‘1A‘.isdecimal())    #是否是十進位
print(‘1A‘.isdigit())     #是否是整數
print(‘1A‘.isidentifier())  #判斷是否是一個合法的標識符
print(‘33‘.isnumeric())     #判斷是否是一個數字
print(‘ ‘.isspace())      #判斷是否是一個空格
print(‘My Name Is‘.istitle())    #判斷是標題(每個首字母大寫)
print(‘My Name Is‘.isprintable())  #判斷是否是列印(tty file,drive file)
print(‘MY NAME IS‘.isupper())      #判斷是否全部是大寫

print(‘+‘.join([‘1‘,‘2‘,‘3‘]))     #將列表的元素串連起來
print(name.ljust(50,‘*‘))            #長度不足50,用*右補全
print(name.rjust(50,‘*‘))              #長度不足50,用*左補全
print(‘LanHan‘.lower())              #將大寫變小寫
print(‘Lanhan‘.upper())              #將小寫變大寫
print(\nLanhan‘.lstrip())           #去掉最左邊的空格或斷行符號
print(‘Lanhan\n.rstrip())            #去掉最右邊的空格或斷行符號
print(‘+++++++++++++‘)
print(‘ Lanhan\n.strip())            #去掉最左邊或最右邊的空格或斷行符號
print(‘-----‘)

p = str.maketrans("abcdef",‘123456‘)   #將字串lanhan用對應關係替換a用1替換,b用2替換,依次類推(用於隨機密碼)
print("lanhan".translate(p))
print(‘lanhan‘.replace(‘a‘,‘A‘,1))    #替換字元,1是只替換一個
print(‘lanhan‘.rfind(‘a‘))             #找字元的下標(如有相同字元,找最右邊的一個字元)
print(‘1+2+3+4‘.split(‘+‘))            #以+分割
print(‘1+2\n+3+4‘.splitlines())        #以分行符號分割
print(‘LanHan‘.swapcase())              #大寫換小寫,小寫換大寫
print(‘lan Han‘.title())                #將字串首字母換成大寫
print(‘lanhan‘.zfill(50))               #不夠位,用0補充

 

 

 

註:字串不能修改,只能覆蓋之前的值

python之路-06-字串操作

聯繫我們

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