Python字串全解

來源:互聯網
上載者:User

標籤:inf   字串搜尋   dig   control   section   strip()   超過   其他   san   

1.字串大小寫轉換
1 def strChange():2     str = "[email protected]"3     print("原字串:" + str)4     print("字母寫轉換成小寫:" + str.lower())5     print("字母寫轉換成大寫:" + str.upper())6     print("大寫轉換成小寫,小寫轉換成大寫:" + str.swapcase())7     print("首字母大寫:" + str.title())

2.字串測試
 1 def strTest(): 2     str = "[email protected]" 3     print("原始字串:" + str) 4     print("是否全是字母:s.isalpha=%s" % str.isalpha()) 5     print("是否全是數字:s.isdigit=%s" % str.isdigit()) 6     print("是否全是空白字元:s.isdigit=%s" % str.isspace()) 7     print("字母中是否全是小寫:s.islower=%s" % str.islower()) 8     print("字母中是否全是大寫:s.isupper=%s" % str.isupper()) 9     str = "Niuxinlong"  # 注意此處“[email protected]”是檢測不出來的,字串中字母之間不能包含其他字元(非字母)10     print("是否是首字母大寫:s.istitle=%s" % str.istitle())

3.字串分割與組合
 1 def strSplit(): 2     str = "I love JuJingyi!" 3     print(str.split())  # 預設分隔符號為空白字元,分割為列表形式 4     print(str.split("i", 2))  # 以字元"i"為分割符,分割2次。如果分割次數超過字串最多分割的次數則分割最多的次數 5     strList = ["hello", "Word", "!"] 6     print("".join(str))  # 分割符.join()實現字串的組合 7     print(" ".join(strList)) 8     print("#".join(strList)) 9     str = "Hello!"10     print(" ".join(str))  # 如果join()方法的參數是字串,預設分割符串連每個字元

4.字串搜尋和替換
1 def strFind():2     str = " I love ju Jingyi! "3     print(str.find("i"))  # 列印返回的第一"i"的標號,無則返回-14     print(str.count("i"))  # 計算字元"i"在字串中出現的次數5     print(str.replace("j", "J", 1))  # 將字串中的小寫"j"替換為大寫"J"6     print(str.lstrip())  # 去掉字串左邊的空格7     print(str.rstrip())  # 去掉字串右邊的空格8     print(str.strip())  # 去掉字串左右的空格

if __name__ == "__main__":

  print("---------字串大小寫轉換---------")
  strChange()
  print("\n---------字串測試---------")
  strTest()
  print("\n---------字串分割與組合---------")
  strSplit()
  print("\n---------字串搜尋和替換---------")
  strFind()關聯部落格(CSDN):80207546

 

  

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.