日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python3 字符串方法

發布時間:2023/12/10 python 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python3 字符串方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
python3 字符串方法

1.capitalize() 將字符串的第一個字符改為大寫

1 >>> s='i love cnblog' 2 >>> s.capitalize() 3 'I love cnblog'

2.casefold() 將字符串所有字符改為小寫

1 >>> (s.capitalize()).casefold() 2 'i love cnblog'

3.center(width) 將字符串居中,并用空格將字符串填充至width長度,空格均勻分布在兩側,當width<len(s)時沒有效果

1 >>> s.center(20) 2 ' i love cnblog '

4.count(sub[,start[,end]]) 返回sub在字符串里出現的次數,start,end為可選參數,決定范圍

1 >>> s.count('0',5,13) 2 0 3 >>> s.count('o',5,13) 4 1 5 >>> s.count('o',0,13) 6 2

5.encode(encoding='utf-8',errors='strict') 以encoding指定的編碼格式對字符串進行編碼

1 >>> s.encode(encoding='utf-8',errors='strict') 2 b'i love cnblog' 1 >>> s.encode() 2 b'i love cnblog'

6.endswith(sub[,start[,end]]) 檢查字符串是否以sub結尾,是返回True,否返回False,start,end為可選參數,決定范圍

1 >>> s.endswith('!',0,13) 2 False 3 >>> s.endswith('g') 4 True

7.expandtabs([tabsize=8]) 把字符串的tab字符(\t)轉化為空格,如不指定tabsize,默認為8個空格

1 >>> s.expandtabs() 2 'i love cnblog' 3 >>> s='\t i love cnblog\t' 4 >>> s.expandtabs() 5 ' i love cnblog '

這里第一個\t轉化為8個空格,第二個tab是在后面加了3個空格,與'cnblog'相加共8個字符,并不是直接加8個空格

8.find(sub[,start[,end]]) 檢測sub是否在字符串中,如果在則返回index,否則返回-1,start,end為可選參數,決定范圍

1 >>> s='i love cnblog' 2 >>> s.find('o') 3 3 4 >>> s.find('o',3,13) 5 3 6 >>> s.find('o',4,13) 7 11 8 >>> s.find('g',0,13) 9 12

這里返回的是sub的index,同時start,end都是包含的。

?9.index(sub[,start[,end]]) 類似find(),不同在于如果sub不在字符串中,返回的不是-1而是異常

1 >>> s='i love cnblog' 2 >>> s.index('o') 3 3 4 >>> s.index('h') 5 Traceback (most recent call last): 6 File "<pyshell#2>", line 1, in <module> 7 s.index('h') 8 ValueError: substring not found

10.isalnum() 如果字符串至少有一個字符,并且所有字符都是字母或數字則返回True,否則False

1 >>> s='i love cnblog'#有空格 2 >>> s.isalnum() 3 False 4 >>> s='ilovecnblog' 5 >>> s.isalnum() 6 True 7 >>> s='11ii' 8 >>> s.isalnum() 9 True

11.isalpha() 如果字符串至少有一個字符,并且所有字符都是字母則返回True,否則False

1 >>> s='ilovecnblog' 2 >>> s.isalpha() 3 True

12.isdigit() 如果字符串只包含數字則返回True,否則返回False

1 >>> s='1234' 2 >>> s.isdigit() 3 True

13.isdecimal() 如果字符串只包含十進制數字則返回True,否則返回False

>>> s='1234' >>> s.isdecimal() True >>> s='ox12'#十六進制 >>> s.isdecimal() False >>> s='o123'#八進制 >>> s.isdigit() False

14.islower() 如果字符中至少包含一個能區分大小寫的字符,并且這些字符都是小寫則返回True,否則返回Flase

 isupper()如果字符中至少包含一個能區分大小寫的字符,并且這些字符都是大寫則返回True,否則返回Flase

1 >>> s='ilovecnblog' 2 >>> s.islower() 3 True 4 >>> s='ILOVE' 5 >>> s.isupper() 6 True

15.isnumeric() 如果字符串只包含數字字符,則返回True,否則返回False

初一看感覺和isdigit()是一樣的,但是:

1 >>> num='1' 2 >>> num.isdigit() 3 True 4 >>> num.isnumeric() 5 True 6 >>> num=b'1' 7 >>> num.isdigit() 8 True 9 >>> num.isnumeric() 10 Traceback (most recent call last): 11 File "<pyshell#31>", line 1, in <module> 12 num.isnumeric() 13 AttributeError: 'bytes' object has no attribute 'isnumeric' 14 >>> num=''#漢字的數字,同樣的還有羅馬數字等 15 >>> num.isdigit() 16 False 17 >>> num.isnumeric() 18 True

17.isidentifier() 判斷字符串是否包含該語言的保留字

'def'.isidentifier() Out[3]: True 'eval'.isidentifier() Out[4]: True

?18.isprintable() 判斷字符串中所有的字符串都是可以通過repr表示成字符串,或者字符串是空的,都返回True,否則返回False

chr(1000000).isprintable() Out[13]: False

?這里使用一個超出字符編碼范圍的數字去轉化成字符,測試其是否可以打印,顯然,答案是不行。

19.isspace() 判斷字符串,至少有一個字符的字符串中所有字符是否都是空格,不是則返回False

''.isspace() Out[14]: False ' '.isspace() Out[15]: True ' a'.isspace() Out[16]: False

?20.istitle() 判斷是否是標題格式,這里理解為首字母大寫。

'Author'.istitle() Out[17]: True 'aA'.istitle() Out[18]: False 'Aa'.istitle() Out[19]: True 'AAAa'.istitle() Out[20]: False

?21.isupper() 判斷字符串是否全部是大寫

'AAAa'.isupper() Out[21]: False

?22.join() 返回一個用指定字符串分隔的字,或者是將指定字符加入到另一個字符中。

a = '12345' ','.join(a) Out[23]: '1,2,3,4,5'

?23.lower() 返回的是指定字符串的拷貝,并轉化成小寫

'AAA'.lower() Out[24]: 'aaa'

?24.ljust() 可以指定寬度,以及填充字符串,返回的是按寬度,填充字符串格式化后的左對齊的字符串。

b = 'a'.ljust(10) len(b) Out[28]: 10 'a'.ljust(10, 'A') # 指定以A填充 Out[30]: 'aAAAAAAAAA'

?25.partition:在指定字符串中查找sep,如果找到了返回該字符前面的部分,sep,及后面的部分,

如果沒找到則返回sep及兩個空字符中,類似于split,但又有不同

'ssaafdaf'.partition('f') Out[3]: ('ssaa', 'f', 'daf')

?26.replace ,用指定字符串替換指定字符串,如果不指定替換次數,僅替換第一個。

'this is a test'.replace('a', 'A') Out[4]: 'this is A test'

?27.rfind(): 返回指定子串的最高索引,如果沒找到則返回-1,可以指定要開始替換的起始,結束位置。

'this is a test'.rfind('i') Out[5]: 5

?28.rindex(),與上面的rfind一樣,只是如果沒找到不是返回-1,而是觸發錯誤

'this is a test'.rindex('g') Traceback (most recent call last):File "C:\Program Files\Python35\lib\site-packages\IPython\core\interactiveshell.py", line 2869, in run_codeexec(code_obj, self.user_global_ns, self.user_ns)File "<ipython-input-6-f8a393e6a7d2>", line 1, in <module>'this is a test'.rindex('g') ValueError: substring not found

?29.rjust();與ljust()相對應

'aa'.ljust(10) Out[25]: 'aa ' 'aa'.rjust(10) Out[26]: ' aa'

?30.rpartition()與partition一樣,但是是從右邊開始

'this is a test'.rpartition('a') Out[7]: ('this is ', 'a', ' test')

?31.rsplit(),與split作用相同,但是從右側開始

this is a test'.rsplit(' ') Out[8]: ['this', 'is', 'a', 'test']

?但是講真,如果不仔細考慮你是不會發現它與split有什么不同的,只有當你指定了最大切割次數時才會有效果。

'this is a test'.split(' ', 1) Out[10]: ['this', 'is a test']
'this is a test'.rsplit(' ', 1)
Out[11]:
['this is a', 'test']

?32.rstrip(), 從右側移除指定字符

'this is a test'.rstrip('t') Out[12]: 'this is a tes'

?33.split(), 按指定字符串對目標字符串進行切割,可以指定切割次數

'this is a test'.split('i', 1) Out[13]: ['th', 's is a test']

?感覺它與partition的不同在于它返回的結果中移除了指定的字符串

34.splitlines(),返回字符串的行,按換行符切割,如果沒指定keepends=True,則會將其從結果中移除

'this is a string\n this is a test'.splitlines() Out[14]: ['this is a string', ' this is a test']'this is a string\n this is a test'.splitlines(keepends=True) Out[16]: ['this is a string\n', ' this is a test']

?35.startswith(),判斷字符串是否以某個字符開頭

'this is a test'.startswith('t') Out[18]: True # 不一定非得一單個字符 'this is a test'.startswith('this') Out[19]: True

?36.strip() 移除字符串兩側的指定字符串,默認移除空格,需要注意的是可以指定多個字符

'this is a test'.strip('ts') Out[20]: 'his is a te'

?37.swapcase() 轉換大小寫

'this is A test'.swapcase() Out[21]: 'THIS IS a TEST'

?38.title(), 標題格式,就是首字母大寫,其它字符小寫

'this is a test'.title() Out[22]: 'This Is A Test'

?39.upper(),將字符全部轉成大寫

'this is a test'.upper() Out[24]: 'THIS IS A TEST'

?40.zfill(),這里的z指zero,用0將字符填充到指定長度

'aa'.zfill(10) Out[25]: '00000000aa'

?41.maketrans(),translate,因為內容比較多,見我另一博客

http://www.cnblogs.com/Andy963/p/7060292.html

posted on 2015-12-24 15:06 Andy_963 閱讀(...) 評論(...) 編輯 收藏

轉載于:https://www.cnblogs.com/Andy963/p/5073128.html

總結

以上是生活随笔為你收集整理的python3 字符串方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。