python 字串的魔法 day10

來源:互聯網
上載者:User

標籤:中間   大於   div   height   空白   不用   case   包含   center   

首字母大寫

test = "alex"v = test.capitalize()print(v)

所有變小寫,第一個更牛逼,很多的未知的對應關係可以變小寫

test/casefold()

test.lower()

設定寬度,並將文字放中間,20,為總長度,x為空白填充

test = "ALEX"v =test.center(20,"x")print(v)

計算當前文本中某元素個數count,a表示找的元素,5,6表示開頭和結尾

test = "alexaler"v =test.count("a",5,6)print(v)

是否以什麼結尾  shartwith  以什麼開頭

test = "alexaler"v =test.endswith("a")print(v)

找到某字元在字串位置,擷取第一個位置,後面不管,5,8表示從哪到哪,要找到必須大於所在位置,找不到報-1,index作用相同,不過找不到報錯

test = "alexaler"v =test.find("r",5,8)print(v)

格式化,將字串中的預留位置替換為制定的值

test = "al{ex}al{ex}"
#括弧可替換為0,1,2
#下面則不用ex=,直接寫rprint(test)v =test.format(ex="r")print(v)
test = "al{0}al{1}"#括弧可替換為0,1,2#下面則不用ex=,直接寫rprint(test)v =test.format("r","d")print(v)

判斷字串中是否只包含字母和數字

test = "uasf890+"v = test.isalnum()print(v)

 

python 字串的魔法 day10

聯繫我們

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