兄弟們Python都學的怎么樣了?字串學會了么?

字串是Python最基本的資料型別,遍布所有Python程式,你要你在用用Python,就都會使用到它,
所以總結了15個最重要的內置字串方法分享給大家,希望大家能從中找到對自己有幫助的技巧,
1、isalnum()
如果字串中至少有一個字符并且所有字符都是字母或數字,則回傳True,否則回傳 False,
s = 'python' print(s.isalnum()) # True s = '123' print(s.isalnum()) # True s = 'python123' print(s.isalnum()) # True s = 'python-123' print(s.isalnum()) # False # Python學習交流1群:924040232 # Python學習交流2群:815624229 # 一群滿加2群
2、find()
檢測指定內容是否包含在字串中,如果是回傳開始的索引值,否則回傳-1,
s = 'Machine Learning' idx = s.find('a') print(idx) print(s[idx:]) # 1# achine Learning s = 'Machine Learning' idx = s.find('aa') print(idx) print(s[idx:]) # -1# g
此外,還可以指定開始的范圍,
s = 'Machine Learning' idx = s.find('a', 2) print(idx) print(s[idx:]) # 10 # arning
3、count()
回傳指定內容在字串中出現的次數,
n = 'hello world'.count('o') print(n) # 2 n = 'hello world'.count('oo') print(n) # 0
4、join()
string.join(seq),以string作為分隔符,將seq中所有的元素(的字串表示)合并為一個新的字串,
list_of_strings = ['string', 'methods', 'in', 'python'] s = '-'.join(list_of_strings) print(s) # string-methods-in-python list_of_strings = ['string', 'methods', 'in', 'python'] s = ' '.join(list_of_strings) print(s) # string methods in python
5、rsplit()
從右側開始對字串進行分隔
s = 'string methods in python'.rsplit(' ', maxsplit=1) print(s) # ['string methods in', 'python']
6、Slicing
slicing切片,按照一定條件從串列或者元組中取出部分元素(比如特定范圍、索引、分割值)
s = ' hello ' s = s[:] print(s) # hello s = ' hello ' s = s[3:8] print(s) # hello
7、strip()
strip()方法用于移除字串頭尾指定的字符(默認為空格或換行符)或字符序列,
s = ' hello '.strip() print(s) # hello s = '###hello###'.strip() print(s) # ###hello###
在使用strip()方法時,默認去除空格或換行符,所以#號并沒有去除,
可以給strip()方法添加指定字符,如下所示,
s = '###hello###'.strip('#') print(s) # hello
此外當指定內容不在頭尾處時,并不會被去除,
s = ' \n \t hello\n'.strip('\n') print(s) # # hello s = '\n \t hello\n'.strip('\n') print(s) # hello
第一個\n前有個空格,所以只會去取尾部的換行符,
最后strip()方法的引數是剝離其值的所有組合,這個可以看下面這個案例,
s = 'www.baidu.com'.strip('cmow.') print(s) # baidu
最外層的首字符和尾字符引數值將從字串中剝離,字符從前端移除,直到到達一個不包含在字符集中的字串字符為止,
在尾部也會發生類似的動作
8、lstrip()
移除字串左側指定的字符(默認為空格或換行符)或字符序列,
s = ' hello '.lstrip() print(s) # hello
同樣的,可以移除左側所有包含在字符集中的字串,
s = 'Arthur: three!'.lstrip('Arthur: ') print(s) # ee!
9、rstrip()
移除字串右側指定的字符(默認為空格或換行符)或字符序列,
s = ' hello '.rstrip() print(s) # hello
10、removeprefix()
Python3.9中移除前綴的函式,
# python 3.9 s = 'Arthur: three!'.removeprefix('Arthur: ') print(s) # three!
和strip()相比,并不會把字符集中的字串進行逐個匹配,
11、removesuffix()
Python3.9中移除后綴的函式,
s = 'HelloPython'.removesuffix('Python') print(s) # Hello
12、replace()
把字串中的內容替換成指定的內容,
s = 'string methods in python'.replace(' ', '-') print(s) # string-methods-in-python s = 'string methods in python'.replace(' ', '') print(s) # stringmethodsinpython
13、re.sub()
re是正則的運算式,sub是substitute表示替換,
re.sub則是相對復雜點的替換,
import re s = "string methods in python" s2 = s.replace(' ', '-') print(s2) # string----methods-in-python s = "string methods in python" s2 = re.sub("\s+", "-", s) print(s2) # string-methods-in-python
和replace()做對比,使用re.sub()進行替換操作,確實更高級點,
14、split()
對字串做分隔處理,最終的結果是一個串列,
s = 'string methods in python'.split() print(s) # ['string', 'methods', 'in', 'python']
當不指定分隔符時,默認按空格分隔,
s = 'string methods in python'.split(',') print(s) # ['string methods in python']
15、upper()
將字串中的字母,全部轉換為小寫,
s = 'SIMPLE IS BETTER THAN COMPLEX'.lower() print(s) # simple is better than complex
兄弟們,今天的分享就到這里,大家學廢了嗎?
學習難,難在堅持!與諸君共勉!加油!

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/440362.html
標籤:Python
