python之字符串函數

1.  endswith()  startswith()python

1 # 以什麼什麼結尾
2 # 以什麼什麼開始
3 test = "alex"
4 v = test.endswith('ex') 5 v = test.startswith('ex') 6 print(v)
View Code

2. expandtabs()git

1 test = "1\t2345678\t9"
2 v = test.expandtabs(6) 3 print(v,len(v))
View Code

3. find()api

1 # 從開始日後找,找到第一個以後,獲取其未知
2 # > 或 >=
3 test = "alexalex"
4 # 未找到 -1
5 v = test.find('e') 6 print(v)
View Code

4.  index()ide

1 # index找不到,報錯 忽略
2 test = "alexalex"
3 v = test.index('a') 4 print(v)
View Code

5. format()  format_map()spa

 1 # 格式化,將一個字符串中的佔位符替換爲指定的值
 2 test = 'i am {name}, age {a}'
 3 print(test)  4 v = test.format(name='alex',a=19)  5 print(v)  6 
 7 test = 'i am {0}, age {1}'
 8 print(test)  9 v = test.format('alex',19) 10 print(v) 11 
12 # 格式化,傳入的值 {"name": 'alex', "a": 19}
13 test = 'i am {name}, age {a}'
14 v1 = test.format(name='df',a=10) 15 print(v1) 16 v2 = test.format_map({"name": 'alex', "a": 19}) 17 print(v2)
View Code

6. isalnum().net

1 # 字符串中是否只包含 字母和數字
2 test = "er;"
3 v = test.isalnum() 4 print(v)
View Code

 7.capitalize() 首字母大寫3d

1 test = "sqy"
2 v = test.capitalize() 3 print(v)
View Code

8. casefold()  lower() 全部變小寫,casefold更牛逼,不少未知的對相應變小寫code

1 test = "sqy"
2 v1 = test.casefold() 3 print(v1) 4 v2 = test.lower() 5 print(v2)
View Code

9. center() 置寬度,並將內容居中orm

1 # 20 代指總長度 2 # * 空白未知填充,一個字符,無關緊要 3 test = "sqy"
4 v = test.center(20,"中") 5 print(v)
View Code

10.  ljust() rjust() zfill()blog

 1 # test = "alex"
 2 # v = test.ljust(20,"*")  3 # print(v)  4 
 5 # test = "alex"
 6 # v = test.rjust(20,"*")  7 # print(v)  8 
 9 # test = "alex"
10 # v = test.zfill(20) 11 # print(v)
View Code

11. count() 去字符串中尋找,尋找子序列的出現次數

1 test = "aLexalexr"
2 v = test.count('ex') 3 print(v) 4 
5 test = "aLexalexr"
6 v = test.count('ex',5,6) 7 print(v)
View Code

12. isalpha() 是不是字母,漢子

1 test = "as大多數df"
2 v = test.isalpha() 3 print(v)
View Code

13. isdecimal()  isdigit() isnumeric()  當前輸入是不是數字

1 test = "②" # 1,②,兒 2 v1 = test.isdecimal() 3 v2 = test.isdigit() 4 v3 = test.isnumeric() 5 print(v1,v2,v3)
View Code

14.isprintable() 是否存在不可顯示的字符

1 # \t 製表符 2 # \n 換行 3 test = "gdds\tfdsfd"
4 v = test.isprintable() 5 print(v)
View Code

15. isspace()  判斷是否所有是空格

1 test = "  ass"
2 v = test.isspace() 3 print(v)
View Code

16.  istitle() title() 判斷是不是標題

1 test = "Return True if all cased characters in S are uppercase and there is"
2 v1 = test.istitle() 3 print(v1) 4 v2 = test.title() 5 print(v2) 6 v3 = v2.istitle() 7 print(v3)
View Code

17. join() 將字符串中的每個元素按照指定分隔符進行拼接

1 test = "你是風兒我是沙"
2 print(test) 3 t = ' '
4 v = "_".join(test) 5 print(v)
View Code

18.  islower() lower()  isupper() upper()

1 test = "Alex"
2 v1 = test.islower() 3 v2 = test.lower() 4 print(v1, v2) 5 
6 v1 = test.isupper() 7 v2 = test.upper() 8 print(v1,v2)
View Code

19. lstrip()  rstrip() strip( 移除指定字符串 有限最多匹配

 1 test = "\nxas12xa\n\txax1221axa  "
 2 v = test.lstrip('xa')  3 v = test.rstrip('9lexxexa')  4 v = test.strip('xa')  5 print(v)  6 
 7 test.lstrip()  8 test.rstrip()  9 test.strip() 10 # 去除左右空白 11 v = test.lstrip() 12 v = test.rstrip() 13 v = test.strip() 14 print(v) 15 print(test) 16 # 去除\t \n 17 v = test.lstrip() 18 v = test.rstrip() 19 v = test.strip() 20 print(v)
View Code

20. maketrans()  translate()  對應關係替換

1 v = "asidufkasd;fiuadkf;adfkjalsdjf"
2 m = str.maketrans("aeiou", "12345") 3 new_v = v.translate(m) 4 print(new_v)
View Code

21. partition()  rpartition() 分割爲三部分

1 test = "testasdsddfg"
2 v = test.partition('s') 3 print(v) 4 v = test.rpartition('s') 5 print(v)
View Code

22. split() rsplit()  splitlines()

 1 test = "sqys"
 2 v = test.split('s',2)  3 print(v)  4 v = test.rsplit()  5 print(v)  6 
 7 
 8 # 23 分割,只能根據,truefalse:是否保留換行  9 # test = "asdfadfasdf\nasdfasdf\nadfasdf"
10 # v = test.splitlines(False) 11 # print(v)
View Code

23. swapcase() 大小寫轉換

1 test = "aLex"
2 v = test.swapcase() 3 print(v)
View Code

24. isidentifier() 字母,數字,下劃線 : 標識符 def class

1 a = "de12_f"
2 v = a.isidentifier() 3 print(v)
View Code

25.replace()  將指定字符串替換爲指定字符串

1 test = "alexalexalex"
2 v = test.replace("ex",'bbb') 3 print(v) 4 v = test.replace("ex",'bbb',2) 5 print(v)
View Code

 26.灰魔法

  1、for循環   

 1 test = "返回到合肥東方紅剛纔修改"
 2 # for 變量名 in 字符串:
 3 # 變量名
 4 # break
 5 # continue
 6 
 7 
 8 index = 0  9 while index < len(test): 10     v = test[index] 11     print(v) 12 
13     index += 1
14 print('=======') 15 
16 for zjw in test: 17     print(zjw) 18 
19 test = "好方法個地方個地方剛發的"
20 for item in test: 21     print(item) 22     break
23 
24 for item in test: 25     continue
26     print(item)
View Code

  2、索引,下標,獲取字符串中的某一個字符

1 test = "好方法個地方個地方剛發的"
2 v = test[3] 3 print(v)
View Code

  3、切片

1 test = "好方法個地方個地方剛發的"
2 v = test[0:10] # 0=< <1
3 print(v)
View Code

  4、獲取長度

1 # Python3: len獲取當前字符串中由幾個字符組成
2 test = "好方法個地方個地方剛發的"
3 v = len(test) 4 print(v)
View Code

27. range()  獲取連續或不連續的數字

 1 # Python2中直接建立在內容中
 2 # python3中只有for循環時,才一個一個建立
 3 v = range(10)  4 v = range(3,10)  5 v = range(1,10,2)  6 # 幫助建立連續的數字,經過設置步長來指定不連續
 7 v = range(0, 100, 5)  8 
 9 for item in v: 10     print(item)
View Code

 28.zip()

 1 # print(list(zip(('a','n','c'),(1,2,3))))
 2 # print(list(zip(('a','n','c'),(1,2,3,4))))
 3 # print(list(zip(('a','n','c','d'),(1,2,3))))
 4 #  5 # p={'name':'alex','age':18,'gender':'none'}
 6 # print(list(zip(p.keys(),p.values())))
 7 # print(list(p.keys()))
 8 # print(list(p.values()))
 9 # 10 print(list(zip(['a','b','c'],'12345')))
View Code
相關文章
相關標籤/搜索