Python 字串的所有方法詳解

來源:互聯網
上載者:User

標籤:make   cas   map   lines   自己   pca   logs   轉換   space   

 1 name = "my name is {name} and my age is {age}" 2  3 # 首字母大寫 4 name.capitalize() 5 # 統計某個字元的個數 6 name.count("a") 7 # name 放到中間一共列印50個字元,不夠用 - 補上 8 name.center(50, "-") 9 # 以什麼字元結尾 返回 Boolean 類型10 name.endswith("ai")11 # tab 長度12 name.expandtabs()13 # 取某個字元的索引,從0開始14 print(name.find("y"))15 16 print(name.format(name="xiaosai", age=18))17 print(name.format_map({"name": "xiaosai", "age": 18}))18 19 # 是不是一個阿拉伯數字(包含所有的英文字元 and 1~9)20 print(name.isalnum())21 # 是不是純英文字元(大小寫)22 print(name.isalpha())23 # 是不是小數(忘記吧)24 # print(.isdecimal())25 # 是不是整數26 print(name.isdigit())27 28 # 判斷是不是一個合法的標識符(是不是一個合法的變數名)29 print("3Sink".isidentifier())30 # 是不是小寫31 print("sink".islower())32 # 是不是一個數字33 print("36663".isnumeric())34 # 是不是空格35 print("36663".isspace())36 # 是不是Title (單詞每個首字母大寫)37 print("My Name Is Koi".istitle())38 # 忘記  tty file ,drive file39 print("My Name Is Koi".isprintable())40 # 是不是大寫41 print("XIAOSAI".isupper())42 # jone (1-2-3)43 print(‘-‘.join([‘1‘, ‘2‘, ‘3‘]))44 # 右邊45 print(name.ljust(50, "-"))46 # 左邊47 print(name.rjust(50, "-"))48 49 # 大小寫轉換50 print("NiuMoWang".lower())51 print("NiuMoWang".upper())52 53 # strip 是去兩邊,l 去左邊,r去右邊54 print("\nname\n".strip())55 print("\nname".lstrip())56 print("name\n".rstrip())57 58 # 自己嘗試(類似於加密)59 p = str.maketrans("kaieuq", "123456")60 print("xiaosai".translate(p))61 62 print("xiaosai".replace("a", "A", 1))63 # 找最後邊字元的座標64 print("xiaosai".rfind("a"))65 # 分割66 print("xiao,sai".split(","))67 # 按照換行分割68 print("xiao\ns\na\ni".splitlines())69 70 print("xiao sai".swapcase())71 # 首字母大寫,變成title72 print("xiao sai".title())73 74 print("xiaosai".zfill(30))

 

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.