python字串常用操作方法

2021-09-12 16:02:50 字數 4172 閱讀 7729

1.去除空格

str.strip():刪除字串兩邊的指定字元,括號的寫入指定字元,預設為空格

>>> a='   hello   '

>>> b=a.strip()

>>> print(b)

hello

str.lstrip():刪除字串左邊的指定字元,括號的寫入指定字元,預設為空格

>>> a='   hello   '

>>> b=a.lstrip()

>>> print(b)

hello #右邊空格可能看的不是很明顯

str.rstrip():刪除字串右邊指定字元,預設為空格

>>> a='   hello   '

>>> b=a.rstrip()

>>> print(b)

hello

2.複製字串

>>> a='hello world'

>>> b=a

>>> print(a,b)

hello world hello world

3.連線字串

1 +:連線2個字串

2 >>> a='hello '

3 >>> b='world'

4 >>> print(a+b)

5 hello world

6 注:此方法又稱為 "萬惡的加號",因為使用加號連線2個字串會呼叫靜態函式string_concat(register pystringobject *a ,register pyobject * b),在這個函式中會開闢一塊大小是a+b的記憶體的和的儲存單元,然後將a,b字串拷貝進去。如果是n個字串相連 那麼會開闢n-1次記憶體,是非常耗費資源的。

7 8 str.join:連線2個字串,可指定連線符號(關於join,讀者可以自己去檢視一些相關資料)

9 >>> a='hello '

10 >>> b='####'

11 >>> a.join(b)

12 '#hello #hello #hello #'

4.查詢字串

1 #str.index 和str.find 功能相同,區別在於find()查詢失敗會返回-1,不會影響程式執行。一般用find!=-1或者find>-1來作為判斷條件。

2 str.index:檢測字串中是否包含子字串str,可指定範圍

3 a='hello world'

4 >>> a.index('l')

5 26 >>> a.index('x')

7 traceback (most recent call last):

8 file "", line 1, in 9 a.index('x')

10 valueerror: substring not found

11 str.find:檢測字串中是否包含子字串str,可指定範圍

12 13 >>> a='hello world'

14 >>> a.find('l')

15 2

16 >>> a.find('x')

17 -1

5.比較字串

str.cmp:比較兩個物件,並根據結果返回乙個整數。x< y,返回值是負數 ,x>y 返回的值為正數。

1 >>> a=100

2 >>> b=80

3 >>> cmp(a,b)

4 1

6.是否包含指定字串

1 in |not in 

2 >>> a='hello world'

3 >>> 'hello' in a

4 true

5 >>> '123' not in a

6 true

7.字串長度

1 str.len

2 >>> a='hello world'

3 >>> print(len(a))

4 11

8.字串中字母大小寫轉換

1 s.lower() #轉換為小寫 

2 >>> a='hello world'

3 >>> print(a.lower())

4 hello world

5 6 s.upper() #轉換為大寫

7 >>> a='hello world'

8 >>> print(a.upper())

9 hello world

10 11

12 s.swapcase() #大小寫互換

13 >>> a='hello world'

14 >>> print(a.swapcase())

15 hello world

16 17 s.capitalize() #首字母大寫

18 >>> a='hello world'

19 >>> print(a.capitalize())

20 hello world

9.將字串放入中心位置可指定長度以及位置兩邊字元

1 str.center()

2 >>> a='hello world'

3 >>> print(a.center(40,'*'))

4 **************hello world***************

10.字串統計

>>> a='hello world'

>>> print(a.count('l'))

3

11.字串的測試、判斷函式,這一類函式在string模組中沒有,這些函式返回的都是bool值

複製**

1 s.startswith(prefix[,start[,end]]) #是否以prefix開頭

2 s.endswith(suffix[,start[,end]]) #以suffix結尾

3 s.isalnum() #是否全是字母和數字,並至少有乙個字元

4 s.isalpha() #是否全是字母,並至少有乙個字元

5 s.isdigit() #是否全是數字,並至少有乙個字元

6 s.isspace() #是否全是空白字元,並至少有乙個字元

7 s.islower() #s中的字母是否全是小寫

8 s.isupper() #s中的字母是否便是大寫

9 s.istitle() #s是否是首字母大寫的

12.字串切片

1 str = '0123456789′

2 print str[0:3] #擷取第一位到第三位的字元

3 print str[:] #擷取字串的全部字元

4 print str[6:] #擷取第七個字元到結尾

5 print str[:-3] #擷取從頭開始到倒數第三個字元之前

6 print str[2] #擷取第三個字元

7 print str[-1] #擷取倒數第乙個字元

8 print str[::-1] #創造乙個與原字串順序相反的字串

9 print str[-3:-1] #擷取倒數第三位與倒數第一位之前的字元

10 print str[-3:] #擷取倒數第三位到結尾

11 print str[:-5:-3] #逆序擷取,擷取倒數第五位數與倒數第三位數之間

python字串的常用操作方法

captilze 首字母大寫 swapcase 大小寫反轉 upper 全部大寫 lower 全部小寫 find 通過元素找索引,找到第乙個就返回,可以切片,找不到返回 1.index 通過元素找索引,找到第乙個就返回,可以切片,找不到報錯.replace old,new,count 替換。cent...

Python 字串操作方法

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

python 字串操作方法

字串物件的操作方法 序列操作方法 內建函式或表示式,如lenth 和型別特定方法 物件方法呼叫,如s.find 說明 模式 pattern 匹配是指正規表示式,re模組。而文字串分隔符就是簡單的字串。字串分割 str.split python內建函式,返回值為列表,只能傳入單一的文字串分隔符,如st...