索引就是下標,下標從0開始php
s1 = 'python' print(s1[0]) # 獲取第一個 print(s1[6]) #越界,報錯 print(s1[-1]) # 獲取倒數第一個 print(s1[-2]) # 獲取倒數第二個
s2 = "python" print(s2[0:3]) # pyt print(s2[4:6]) # on print(s2[3:]) # hon print(s2[-1:-3]) # 從-1獲取到-5這樣是獲取不到任何結果的,默認從左往右數 print(s2[-3:-1]) # ho print(s2[:]) # python
步長:java
步長若是是正數,則從左往右取,若是是負數,則從右往左取。默認爲1python
s3 = ""python" print(s3[1:5:2]) # yh print(s3[-1:-5:-1]) #noht print(s3[-2::-2]) # otp
功能:對第一個單詞首字母大寫,對於非第一個單詞首字母進行檢查,若大寫則改成小寫。c++
s1 = "hello world" print(s1.capitalize()) # Hello world
s2 = "heLLo"
print(s2.capitalize()) # Hello
功能:將字符串所有轉換成小寫git
s1 = "HeLLo WoRld" print(s1.lower()) # hello world
功能:將字符串所有轉換成大寫c#
s1 = "hello world" print(s1.upper()) # HELLO WORLD
功能:大小寫互相轉換api
s1 = "Hello World" print(s1.swapcase()) # hELLO wORLD
功能:轉換成小寫函數
和lower的區別:lower()對某些字符支持不夠好。casefold()對全部的字母都有效。好比東歐的一些字母。spa
s2 = "БBß" # 俄美德字符 print(s2.casefold()) # 6bss print(s2.lower()) # 6bß
功能:每一個被特殊字符隔開的字母首字母大寫code
s1 = "hello world" s2 = "pyt你好hon" print(s1.title()) # Hello World print(s2.title()) # Pyt你好Hon
功能:居中
s1 = "python" print(s1.center(10)) # python print(s1.center(10, "*")) # **python**
功能:更改tab的長度,默認長度改成8
s1 = 「pyth\ton」 print(s1.expandtabs()) # pyth on
功能:默認去掉左右兩端的空白(空格,\n, \t)
s1 = " \n python \t " print(s1.strip()) # python
s2 = "hello world"
print(s2.lstrip("h").rstrip("d") # ello worl
功能:默認去掉左端的空白(空格,\n, \t)
功能:默認去掉右端的空白(空格,\n, \t)
功能:字符串替換
s1 = "hello world" ret = s1.replace("o", "p") # hellp wprld ret = s1.replace("o", "p",1) # hellp world
功能:字符串切割
s1 = "python, java, c, c++, c#" print(s1.split(",") # ["python", "java", "c", "c++", "c#"
s = "py|th|on"
print(s.split("|", 1)) # ['py', 'th|on']
print(int(" 1 ")) # 1 int()會自動去除左右兩端的空白
join
功能:將列表名元素按給定字符鏈接
s = "hello" ss = "_".join(s) print(ss) # h_e_l_l_o
lst = ["hello", "world"] s = "_".join(lst) print(s) # hello_world
功能:判斷是否以某個/些字符開頭
s1 = "Beautiful is better than ugly" print(s1.startswith("Beautiful")) # True
功能:判數是否以某個/些字符結尾
s1 = "Beautiful is better than ugly" print(s1.startswith("ugly")) # True
功能:查找某個/些字符出現的次數
s1 = "python" print(s1.count("h")) # 1
print("hello".count("l", 0, 3)) # 1 判斷"hello"[0:3]中"l"出現多少次
功能:查找某個/些字符出現的位置,若是沒有返回-1
若是查找的某個字符在字符串中出現屢次,則查找所得的結果是第一個字符出現的位置。
s1 = "python, java, c, c++, php" print(s1.find("java")) # 8
切片找:
s1 = "python, java, c, c++, php" print(s1.find("java", 12, 25)) # -1
從字符串末尾開始查找
功能:求索引位置,若是找不到索引,程序會報錯
s1 = "python, java, c, c++, php" print(s1.index("java")) # 8
rindex
從字符串末尾開始查找
print("我叫%s, 今年%d歲了, 我喜歡%s"%("賽利亞", 18, "玩")) print("我叫{}, 今年{}歲了, 我喜歡{}".format("賽利亞", 18, "玩")) print("我叫{0}, 今年{2}歲了, 我喜歡{1}".format("賽利亞", "玩", 18)) print("我叫{name}, 今年{age}歲了, 我喜歡{hobby}".format(name="賽利亞", hobby="玩", age=18))
功能:是否由字母和數字(int)組成,能夠理解爲包含isalpha和isnumeric的功能
s1 = "123.16" s2= "abc" s3 = "_abc!@"
s4 = "adfgsddfrd123456789壹貳叄肆伍陸柒一二三"
print(s1.isalnum()) # False print(s2.isalnum()) # True print(s3.isalnum()) # False
print(s4.isalnum()) # True
功能:是否由字母組成
s1 = "123.16" s2= "abc" s3 = "_abc!@"
s4 = "中國"
print(s1.isalpha()) # False print(s2.isalpha()) # True print(s3.isalpha()) # False
print(s4.isalpha()) # True
功能:是否由阿拉伯數字(%d)組成
s1 = "123" s2 = "123.16" print(s1.isdigit()) # True print(s2.isdigit()) # False
a = "123456789壹貳叄肆伍陸柒一二三四五" print(a.isnumeric()) # True
len()是python的內置函數。
s1 = "python" print(len(s1)) # 6
s1 = "hello world" for c in s1: print(c)