日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

字符串的常见方法总结

發布時間:2023/12/16 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 字符串的常见方法总结 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

字符串的常見方法:(人生苦短,我用python)

# 操作字符串的方法有哪些 # print(dir("str")) # dir()用來查看變量可用的方法 # ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',# 'capitalize', 'casefold', 'center', 'count', 'encode', # 'endswith', 'expandtabs', 'find', 'format', 'format_map', # 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', # 'isdigit', 'isidentifier', 'islower', 'isnumeric', # 'isprintable', 'isspace', 'istitle', 'isupper', 'join', # 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', # 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', # 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', # 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']# _方法名_ 這種方法是字符串不能用的 只有前邊沒有下劃線的才能使用

下面介紹一些字符串方法常用的操作:

  • len 方法:求字符串的長度

    str01 = "she is so beautiful,I like her so much!" print(len(str01)) # 39
  • 查找相關方法:

    str01 = "she is so beautiful,I like her so much!" # 01.find() 從左往右 # 02.rfind() 從右往左 # 03.index() 根據索引找 # 04.rindex() 根據索引從右往左找 # 都是返回查找內容所在的索引 print(str01.find("so")) # 7 print(str01.rfind("so")) # 31 print(str01.find("wo")) # -1 print(str01.rfind("wo")) # -1 # 當查找到要查找的內容時,返回查找內容所在的索引;當該方法查不到我們要查找的方法時,返回-1print(str01.index("so")) # 7 print(str01.rindex("so")) # 31 # print(str01.index("wo")) # 報錯 substring not found # print(str01.rindex("wo")) # 報錯 substring not found # 當查找到要查找的內容時,返回查找內容所在的索引;當方法查不到我們想要的結果就報錯
  • count 計算字符串出現的次數

    str01 = "she is so beautiful,I like her so much!" print(str01.count("s", 1, 6)) # 從1開始到6結束(前閉后開)s出現的次數 print(str01.count("s", 1)) # 從1開始到結束“s”出現的次數
  • replace 替代操作,將字符串中的字符用其他字符替代

    str02 = "shell是世界上最好的語言" test = str02.replace("shell", "python") print(test) print(str02) # 字符串是不可變類型 # 注意:replace中有兩個參數:第一個參數代表被替代的字符串,第二個參數代表新字符串
  • split 內容分割:

    str03 = "them love at the first sight!" result01 = str03.split(" ", 2) # 返回列表,從左往右切割兩次 result02 = str03.split(" ") print(type(result01)) print(result01) print(type(result02)) print(result02)# 運行結果: # <class 'list'> # ['them', 'love', 'at the first sight!'] # <class 'list'> # ['them', 'love', 'at', 'the', 'first', 'sight!' # 注意:split有兩個參數:第一個參數代表split根據什么切割,第二個參數代表著被切割成幾段,默認第二個參數為無窮大,直到字符串不能切割為止
  • splitlines 按照行進行分割

    str04 = "平生一顧,最是長情\n生如逆旅,余你難忘" list01 = str04.splitlines() print(list01) # 運行結果: # ['平生一顧,最是長情', '生如逆旅,余你難忘'] # 注意:該方法按照換行符進行切割,切割后存儲于列表中
  • partition 指定字符串作為分割

    str04 = "them love at the first sight!" print(str04.partition(" ")) # 三個元素的元組,從左往右切 print(str04.rpartition(" ")) # 三個元素的元組,從右往左切 # 運行結果: # ('them', ' ', 'love at the first sight!') # ('them love at the first', ' ', 'sight!') # 注意:這里被切割完成之后存儲于元組內元素有三個
  • upper 轉換字母為大寫格式:

    print("good morning".upper()) # GOOD MORNING
  • lower 轉換字母為小寫格式:

    print("GOOD MoRNING".lower()) # good morning
  • capitalize 將第一個單詞的首字母大寫:

    print('good morning sir'.capitalize()) # Good morning sir
  • title 讓每個單詞的首字母大寫

    print('good morning sir'.title()) # Good Morning Sir
  • 案例:upper 和 lower 常用于接受用戶的驗證碼,接受的內容轉換為大寫或者小寫

    while True:content = input("請輸入內容,exit退出:")print("你輸入的內容是:", content)if content.lower() == "exit":break
  • ljust 、rjust 、center 字符串中空格的處理

    # 01.ljust(width,"填充的字符") print("jack".ljust(10, "*")) # jack****** 在右邊補充至十個字符# 02.rjust print("jack".rjust(15, "*")) # ***********jack 在左邊補充至15個字符# 03.center print("jack".center(15, "*")) # ******jack*****
  • strip 去除字符:

    # 01.lstrip 去除左邊的字符串 print("******jack*****".lstrip("*")) # jack*****# 02.rstrip 去除右邊的字符串 print("******jack*****".rstrip("*")) # ******jack# 03.strip 去除兩邊的字符串 str01 = "******jack*****".strip("*") # strip()默認去除空格 print(str01) # jack
  • join 字符串的拼接:

    str01 = "them love at the first sight!" str02 = str01.split(" ") print(str02) # ['them', 'love', 'at', 'the', 'first', 'sight!'] str03 = "@".join(str02) print(str03) # them@love@at@the@first@sight!
  • ord 和 chr 字符串和 ASCLL 碼之間的轉換

    # 將大寫的A轉換為小寫的a str01 = "A" print(chr(ord(str01)+32)) # a
  • 判斷:

    • startswith 是否以什么開始

    • endswith 是否以什么結束

    • isalpha 是否是由字符組成

    • isdigit 是否全是數字組成

    • isalnum 是否是由字符和數字組成

    • isspace 是否是空格

  • list01 = ["clfgash", "68dsad", "dsad255=/", "515Pds", "dsada5d4sa7435$", " ", "y", "I love you", 5525248, "I Mis You", "Tdf","Y"] list_num = [] # 存放全是數字組成的元素 list_str = [] # 存放全是字母組成的元素 list_both = [] # 存放由字母和數字組成的元素 list_space = [] # 存放空格元素 list_upper = [] # 存放單個字母大寫的元素 list_lower = [] # 存放第一個字母小寫的元素 list_title = [] # 存放單詞字母大寫的元素 list_capitalize = [] # 存放第一個字母大寫的元素# 遍歷列表,將列表中的元素分類 for i in list01:# 列表中int型的元素不能使用str中的方法i = str(i)# 判斷第一個字母大寫時,字符串中沒有該方法,# 此時,我們只需要判斷的字符串下標為零是否大寫即可if i[0].isupper():list_capitalize.append(i)# 判斷字符串中每個單詞大寫if i.istitle():list_title.append(i)# 判斷單個字母大寫if i.isupper():list_upper.append(i)# 判斷第一個字母小寫if i.islower():list_lower.append(i)# 判斷字母串是由字母和數字組成if i.isalnum():list_both.append(i)# 判斷字符全是數字if i.isdigit():list_num.append(i)# 判斷字符串全是字母elif i.isalpha():list_str.append(i)else:pass# 不是有數字和字母組成的else:# 是空格的字符if i.isspace():list_space.append(i)print("全是數字的字符串有{}" .format(list_num)) print("全是字母的字符串有{}" .format(list_str)) print("由數字和字母組成的字符串有{}" .format(list_both)) print("是空格的字符串有{}" .format(list_space)) print("單詞開頭大寫的字符串有{}" .format(list_title)) print("第一個字母是大寫的字符串有{}" .format(list_capitalize)) print("單個字母是大寫的字符串有{}" .format(list_upper)) print("第一個字母是小寫的字符串有{}" .format(list_lower)) # 全是數字的字符串有['5525248'] # 全是字母的字符串有['clfgash', 'y', 'Tdf', 'Y'] # 由數字和字母組成的字符串有['clfgash', '68dsad', '515Pds', 'y', '5525248', 'Tdf', 'Y'] # 是空格的字符串有[' '] # 單詞開頭大寫的字符串有['515Pds', 'I Mis You', 'Tdf', 'Y'] # 第一個字母是大寫的字符串有['I love you', 'I Mis You', 'Tdf', 'Y'] # 單個字母是大寫的字符串有['Y'] # 第一個字母是小寫的字符串有['clfgash', '68dsad', 'dsad255=/', 'dsada5d4sa7435$', 'y']

    總結

    以上是生活随笔為你收集整理的字符串的常见方法总结的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。