python 字串常用函式有哪些?

2021-09-27 05:35:04 字數 3469 閱讀 8758

宣告變數

str="hello world"

find() 檢測字串是否包含,返回該字串位置,如果不包含返回-1

str.find("hello") # 返回值:0

str.find("w") # 返回值:6, 這裡需要注意下:空格也是乙個字元。w前面有個空格,所以w位置是6

str.find("r") # 返回值:-1,並不包含在hello world中,如果不包含返回-1

index() 檢測字串是否包含指定的字元,並返回開始的索引值,如果不包含會報錯

str.index("hello") # 返回值:0

str.index("o") # 返回值:4

str.index("w") # 返回值:6

str.index("r") # 返回值:報錯資訊 ,因為r並不包含其中。 所以建議慎用,如果值不存在程式報錯就完蛋了。

len() 返回字串長度,以0開始計算

len(str) #返回值:10

count() 收集指定字元在字串**現的次數

str.count("o") 返回值:2, o字元在hello world中存在兩個。

str.count('o',5,10) 返回值:1, 原因:指定位置後會從索引5開始檢索,以索引10結束。 5到10之間只存在乙個'o'

str.count('o',4,len(str)) 返回值: 2,索引從4開始,到字串結束。len(str)字串長度

replace() 替換字串

str.replace('hello','hello') # 把小寫的hello替換為大寫的hello

str.replace('w','b') # 把w替換為b

split() 字串切割

str.split('o') #以列表的形式返回["hell","w","rld"] ,hello world 裡面的o被切割掉

upper() 將所有的字元轉換為大寫

str.upper() #返回值為 hello world

title() 首字母轉換為大寫

str.title() #返回值:hello world

center() 返回乙個原字串居中,並以空格填充至長度寬度的新字串

str.center(80) #返回值: ( hello world ) 其字串兩頭被空格填充

join() 在字串後面插入乙個指定字元,構造乙個新的字串

str=""

list=["i","love","you"]

_str.join(list) # 返回值: i_love_you 每個列表元素後面都插入乙個下劃線

isspace() 檢測字串中是否只包含空格,如果有返回trun反之返回false,通俗的講就是判斷非空驗證

str=" "

strone="早上好!"

str.isspace() # 返回trun

strone.isspace #返回false

isalnum() 檢測是否只包含數字或字母。用處:可以用於判斷密碼,一般情況下密碼不能輸入漢字或空格

strone="a123"

strtwo="a 456"

strone.isalnum() # 返回trun

strtwo.isalnum() # 返回false ,因為包含空格

isdigit() 檢測字元是否只包含數字, 返回trun 和 false

str='123'

strone='a123'

str.isdigit() 返回trun

str.isdigit() 返回false

isalpha() 檢測字串是否只包含字母

str="abcd"

strone="123abacd"

str.isalpha() # 返回 trun

strone.isalpha() # 返回false

str1.splitlines(keepend=false)

功能:將str1按行切片,並且將切片的結果作為乙個列表返回。keepend預設為false,

當keepend為true的時候,顯示切片的字元【\n】

str2.join(序列)

功能:將序列中的字串以指定的str2進行拼接,並將拼接好的字串返回。

min(str1)

功能:返回str1中的最小字元【比較的是ascii碼值】

max(str1)

功能:返回str1中的最大字元

str1.replace(old,new,count)

引數一:被替換的字串

引數二:新的字串

引數三:替換的次數,若不指定預設全部替換

功能:使用new將str1中old字串進行替換,若指定count則替換前count個,

若不指定count,則全部替換。

判斷字串是否以xx開頭

str1.startswith("xx"[,start][,end])

若是以xx開頭則返回true,否則返回false,若指定範圍,則取值範圍為[start,end),

若不指定範圍,則預設整個字串

判斷字串是否以xx結尾

str1.endswith("xx")

若是以xx結尾則返回true,否則返回false,若指定範圍,則取值範圍為[start,end),

若不指定範圍,則預設整個字串

將普通字串轉為二進位制

str1.encode()

將二進位制字串轉為普通字串

str2.decode()

注意:編碼的格式與解碼的格式必須保持一致

對映替換

生成乙個替換的表

dic = str4.maketrans("yn","12")

根據替換表進行對映替換

str1.isalpha()

功能:判斷str1是否為純字母,若是則返回true,否則返回false。

注意:zfx返傭www.fx61.com/brokerlist/zfx.html,此功能沒有考慮中文,中文預設也是字母

str1.isalnum()

功能:判斷str1是否由數字與字母組成,若是則返回true,否則返回false。

注意:此功能沒有考慮中文,中文預設也是字母

str1.isupper()

功能:判斷str1**現的字母是否全部為大寫,若是則返回true,否則返回false

str1.islower()

功能:判斷str1**現的字母是否全部為小寫,若是則返回true,否則返回false

str1.istitle()

功能:判斷str1是否為標題化的字串,若是則返回true,否則返回false

str1.isspace()

功能:判斷str1中是否只包含空白符,若是則返回true,否則返回false。

str1.isdigit():只能識別阿拉伯數字

str1.isdecimal():只能識別阿拉伯數字

str1.isnumeric():除了阿拉伯數字還可以識別中文的一二三

PYTHON字串常用函式

1.find and rfind 從左開始找 title find le 存在返回索引值,不存在 1 從右開始找 title find le 存在返回索引值,不存在 1 2.join 列表轉成字串 join list 3.split 字串轉成列表 ss,aa,cc split ss aa cc 4....

Python字串常用函式

capitalize 把字串的第乙個字元改為大寫 casefold 把整個字串的所有字元改為小寫 center width 將字串居中,並使用空格填充至長度width的新字串 count sub start end 返回sub在字串裡邊出現的次數,start和end引數表示範圍,可選。encode ...

Python 字串常用函式

函式 描述 返回值 str.capitalize 將字串的第乙個字元大寫 str.title 返回標題化的字串,即每個單詞的首字母都大寫 str.upper 全大寫str.lower 全小寫len str 返回字串的長度。用法與其他不同。str.count substring start end 統...