字符串的常见方法总结
生活随笔
收集整理的這篇文章主要介紹了
字符串的常见方法总结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
字符串的常見方法:(人生苦短,我用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 MORNINGlower 轉換字母為小寫格式:
print("GOOD MoRNING".lower()) # good morningcapitalize 將第一個單詞的首字母大寫:
print('good morning sir'.capitalize()) # Good morning sirtitle 讓每個單詞的首字母大寫
print('good morning sir'.title()) # Good Morning Sir案例:upper 和 lower 常用于接受用戶的驗證碼,接受的內容轉換為大寫或者小寫
while True:content = input("請輸入內容,exit退出:")print("你輸入的內容是:", content)if content.lower() == "exit":breakljust 、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) # jackjoin 字符串的拼接:
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 是否是空格
總結
以上是生活随笔為你收集整理的字符串的常见方法总结的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《C语言及程序设计》实践项目——输出小星
- 下一篇: Ims服务架构