1. 程式人生 > >strin 字符串的內置函數

strin 字符串的內置函數

字符 輸出結果 子串 split 程序 bstr 所有 style 統計

count("x")統計字符串的元素的個數
a = "hello kitty"
print (a.count("t"))#統計t出現的個數
輸出結果:
2
endswith("x")判斷以某個字符結尾
a = "python"
print (a.endswith("n"))
print (a.endswith("on"))
print (a.endswith("hon"))
print (a.endswith("nn"))
輸出結果:
True
True
True
False
startswith("x")判斷以某個字符開頭
a = "python"
print (a.startswith("
p")) print (a.startswith("py")) print (a.startswith("pyt")) print (a.startswith("pp")) 輸出結果: True True True False
capitalize()把字符串首寫字母大寫
a = "python"
print (a.capitalize())
輸出結果:
Python
upper()將字符串裏的小寫變成大寫
a = "python"
print (a.upper())
輸出結果:
PYTHON
lower()把字符串大寫變成小寫
a = "PYTHON"
print (a.lower())
輸出結果:
python
swapcase()把字符串裏的大小寫反轉
a = "PyThoN"
print (a.swapcase())
輸出結果:
pYtHOn
isupper()判斷字符串是否大寫
a = "python"
a1 = "PYTHON"

print (a.isupper())
print (a1.isupper())
輸出結果:
False
True
islower()判斷字符串是否小寫
a = "python"
a1 = "PYTHON"
print (a.islower())
print (a1.islower())
輸出結果:
True
False
isalnum()-判斷字符串是否是字母或者數字
a 
= "python" a1 = "123" a2 = "python123" a3 = "中國" a4 = "#$###" print (a.isalnum()) print (a1.isalnum()) print (a2.isalnum()) print (a3.isalnum()) print (a4.isalnum()) 輸出結果: True True True True False
istitle()判斷首寫字母是否為大寫
a = "python"
a1 = "Python"
a2 = "PYTHON"

print (a.istitle())
print (a1.istitle())
print (a2.istitle())
輸出結果:
False
True
Fals
strip()把字符串左右空格去掉
a = " python "
print (a.strip())
輸出結果:
python #已經將左右空格去掉了,這樣看不出來
----------------------
strip()也會把\n換行去掉
a = "python\n"
a1 = "hello"
print (a.strip()) #strip()把換行去掉了
print (a1)
輸出結果:
python
hello
----------------------
a = "python\n"
a1 = "hello"
print (a)#不使用strip(),將會換行
print (a1)
輸出結果:
python

hello
lstrip()去掉字符串左空格
a = " python "
print (a.strip())
輸出結果:
python  #最後面這有空格,這樣看不出來
rstrip()去掉字符串右空格
a = "  python   "
print (a.rstrip())
輸出結果:
  python  #已經將右邊空格去掉了,這看不出來
replace(old,new) 把字符串的內容替換掉
a = "my tang"
print (a.replace("tang", "guo"))
輸出結果:
my guo
split(分割符,分割次數)對字符串進行各種分割
a = "my \ntang         guo\nli"
print (a.split())#不指定以什麽分割的情況下,會默認所有的空格,換行,制表符分割
輸出結果:
[my, tang, guo, li]
-------------------------------------
a = "my tang guo"
print (a.split(" ")) #以空格進行分割
輸出結果:
[my, tang, guo]
--------------------------------------
a = "my tang guo li"
print (a.split(" ",2))#以空格進行分割2次
輸出結果:
[my, tang, guo li]
---------------------------------------
a = "my tang guo li"
print (a.split("a"))#以 a進行分割,a會被切掉
輸出結果:
[my t, ng guo li]
--------------------------------------
可以指定多個字符進行分割
a = "my tang guo li"
print ((a.split("an"))#以 an進行分割,an會被切掉
["my t" ,"g guo li"]
----------------------------------------
isdigit()判斷字符串是否為整型
a = "python"
a1 = "123"
print (a.isdigit())
print (a1.isdigit())
輸出結果:
True
False
rfind("x")查找元素所在的索引位置,從右向左開始找,找到的第一個元素位置
a = "htp"
a1 = "http"
print (a.rfind("t"))
print (a1.rfind("t"))
輸出結果:
1
2
-----------------------------------------------------------------
find("x")從左向右開始找元素,找到的第一個位置,並返回索引位置
a = "htp"
a1 = "http"
print (a.find("t"))
print (a1.find("t"))
輸出結果:
1
1
index("x")從左向右開始找元素的位置,
a = "ppython"
print (a.index("p")) #有多個相同元素時,返回的是第一個找到的索素值
輸出結果:
0
find 與 index看上去功能一樣,其實是有區別的
a = "python"
print (a.index(x))
index 在沒有找到子串的時候會報錯(
ValueError: substring not found),影響程序執行。
print (a.find("x"))
find 在沒有找到子串時,不會報錯,而是會返回-1所以不會影響執行。

strin 字符串的內置函數