Python-字符串
字符串基础
1 字符串定义:用引号引起来的字符集合称之为字符串(单引号,双引号,三双引号,三单引号)
三引号(三单或三双)支持多行或也可表示注释,而单双引只能一行(但是可以加\n)
2 转义字符串:
常见有 \n 回车字符,
\t 下一制表位
\'' 双引号
\' 单引号
\\ 输出斜杠
\b 往前退格(删除前面那个字符)
1 print('hello world') 2 print('hello world\bwujiadong')#删除前一个字符,后面字符往前进一个
[root@localhost ~]# python 1.py hello world hello worlwujiadong
3 原字符串,若不要被看成是转义字符,那么需在字符串前加上r,称此时的字符串为原字符串
字符串运算
加法运算(拼接)
1 a='wu' 2 b='jia' 3 c='dong' 4 print(a+b+c)
[root@localhost ~]# python 1.py wujiadong
乘法运算(重复)
1 a='wu' 2 b='jia' 3 c='dong' 4 print(a*3)
wuwuwu
关系运算(比较:对应字符相比)
1 a='wu' 2 b='jia' 3 c='dong' 4 if a>b: 5 print(a) 6 else: 7 print(b)
[root@localhost ~]# python 1.py wu
in 运算与not in运算 (a in b 表示判断a是不是在b里面)
1 a='wujiadong' 2 b='wu' 3 if 'wu' in a: #or b ,not wu 4 print('true') 5 else: 6 print('false')
[root@localhost ~]# python 1.py true
字符串访问(index索引【0到len()-1】,slice)
1 a='wujiadong'# 0,1,2,3,4,5,6,7,8 2 print(a[0]) 3 print(a[8]) 4 print(a[0]+a[8]) 5 i=0 6 while i<len(a): 7 print(a[i]) 8 i +=1
[root@localhost ~]# python 1.py w g wg w u j i a d o n g
1 a='wujiadong'# 0,1,2,3,4,5,6,7,8 2 i=0 3 while i< len(a): #为什么这里不能 <= 4 if a[i]=='d': #遍历的方式找一个字符 5 print(a[i],i) 6 i=i+1
[root@localhost ~]# python 1.py ('d', 5)
slice切片:str_name[start:end:step]
统计字符串个数及其位置
1 a='wujiadong'*17 2 b='j' 3 print(a.count(b))
oot@localhost ~]# python 1.py 17
1 a='wujiadong'*17 2 b='j' 3 #print(a.count(b)) 4 i=0 5 while i< len(a): 6 if a[i]==b: 7 print(i,a[i]) 8 i +=1
[root@localhost ~]# python 1.py (2, 'j') (11, 'j') (20, 'j') (29, 'j') (38, 'j') (47, 'j') (56, 'j') (65, 'j') (74, 'j') (83, 'j') (92, 'j') (101, 'j') (110, 'j') (119, 'j') (128, 'j') (137, 'j') (146, 'j')
字符串函数【help(str)】
常见函数有find,index,replace,count,split,strip
isspace()
1 s=' wujiadong ' 2 i=0 3 while i<len(s): 4 if s[i].isspace(): # 5 print(i) 6 i += 1
[root@localhost ~]# python 1.py 0 10
find()和rfind()
1 sub='11aa22cc33aa' 2 a=sub.find('aa',0,12) #找到的第一个符合字符的index 3 b=sub.rfind('aa',0,12) #找到最后一个符合的字符的index 4 print(a,b)
[root@localhost ~]# python 1.py (2, 10)
基于前一次位置往后找
1 s= 'hello jeapedu.com'*4 2 sub='eape' 3 i=0 4 pos=-4 #pos= -len(sub) 5 while i<s.count('eape'): 6 pos=s.find(sub,pos+len(sub)) 7 print(pos) 8 i += 1
[root@localhost ~]# python 1.py 7 24 41 58
例:
1 s='http://www.baidu.com http://www.hao123.com http://www.sohu.com http://www.yahu.com' 2 h='http' 3 c='.com' 4 posh=-len(h) 5 posc=-len(c) 6 i=0 7 while i<s.count(h): 8 posh= s.find(h,posh+len(h))#posh= s.find(h,posc+len(h)) 9 posc= s.find(c,posh+len(c))#posc= s.find(c,posh+len(c)) 10 print(posh), 11 print(posc), 12 print(s[posh:posc+len(c)]) 13 i += 1
[root@localhost ~]# python 1.py 0 16 http://www.baidu.com 21 38 http://www.hao123.com 43 58 http://www.sohu.com 63 78 http://www.yahu.com
去掉字符串s前后的空格
1 s=' wujiadong ' 2 i=0 3 h=-1 4 e=-1 5 while i<len(s): 6 if not s[i].isspace(): 7 h=i 8 break 9 i += 1 10 print(h) 11 i=len(s)-1 12 while i>=0: # 从最后一个往第一个遍历 13 if not s[i].isspace(): 14 e=i 15 break 16 i -=1 17 print(e) 18 print(s[h:e+1])
[root@localhost ~]# python 1.py 1 9 wujiadong
strip函数
1 a=' wujiadong ' 2 t="|"+a+'|' 3 print('1',t) 4 t1='|'+a.strip()+'|' 5 print('2',t1) 6 b='ab123456ab' 7 t1=b.strip('ab') 8 print('3',t1) 9 t2=b.strip('a') 10 print('4',t2) 11 b2='123xxx321' 12 t3=b2.strip('321') 13 print('5',t3)
[root@localhost ~]# python 1.py ('1', '| wujiadong |') ('2', '|wujiadong|') ('3', '123456') ('4', 'b123456ab') ('5', 'xxx')
例:
将字符串s中的网址提出来
1 s='*&)%$#(@http://www.baidu.com*&^)^(%$#' 2 a='*&^)(%$#@' 3 i=0 4 h=0 5 while i<len(s): 6 if s[i] not in a: 7 h=i 8 break 9 i +=1 10 s=s[i:] 11 print(s) 12 i=len(s)-1 13 e=0 14 while i>=0: 15 if s[i] not in a: 16 e=i+1 17 break 18 i -=1 19 s=s[:e] 20 print(s)
[root@localhost ~]# python 1.py http://www.baidu.com*&^)^(%$# http://www.baidu.com
字符串的应用
例:用random.randint随机产生一个六位数
1)判断此数里是否含有4和7(都有)
1 import random 2 i=1 3 while i<=10: 4 x=random.randint(100000,1000000) 5 s=str(x) 6 if '4'in s and '7' in s: 7 print(s,'has 4 and 7') 8 i +=1
[root@localhost ~]# python 1.py ('774596', 'has 4 and 7') ('464273', 'has 4 and 7') ('457881', 'has 4 and 7') ('330487', 'has 4 and 7')
2)在没有4和7的情况下,有没有6和8(都有)
1 import random 2 i=1 3 while i<=10: 4 x=random.randint(100000,1000000) 5 s=str(x) 6 if '4'not in s and '7' not in s: 7 #print(s,'has 4 and 7') 8 if '6' in s and '8' in s: 9 print(s,'no 4and7,but have 6and8') 10 i +=1
[root@localhost ~]# python 1.py ('181196', 'no 4and7,but have 6and8') ('858986', 'no 4and7,but have 6and8')
3)若有4和7出现,打印其出现的位置
1 import random 2 i=1 3 while i<=10: 4 x=random.randint(100000,1000000) 5 s=str(x) 6 if '4' in s or '7' in s: 7 print(s) 8 k=0 9 while k < len(s): 10 if s[k]=='4'or s[k]=='7': 11 print(k,s[k]) 12 k +=1 13 i +=1
[root@localhost ~]# python 1.py 379467 (1, '7') (3, '4') (5, '7') 950484 (3, '4') (5, '4') 865870 (4, '7') 357986 (2, '7') 745119 (0, '7') (1, '4') 267922 (2, '7') 555634 (5, '4') 429089 (0, '4')
例:打印jeapedu000-jeapedu100
1 i=0 2 while i <=100: 3 if i<10: 4 print('jeapedu00'+str(i)) 5 elif i<100: 6 print('jeapedu0'+str(i)) 7 else: 8 print('jeapedu'+str(i)) 9 i +=1
例:构造a01b02c03.....y25z26
利用chr(ascii转字符)ord(字符转ascii)int(字符串转整型)实现翻译
1 a=chr(65) 2 print(a) 3 a=chr(97) 4 print(a) 5 6 b=ord('a') 7 print(b) 8 b=ord('A') 9 print(b)
1 a=97 2 i=0 3 while i< 26: 4 # print(chr(a+i)) 5 print(chr(a+i)+str(i)) 6 i +=1
如何判断字符串里有‘jeap’这个子字符串?其位置?
1 a= 'hello jeapedu.com' 2 sub='jeap' 3 i=0 4 while i<len(a): 5 if a[i]==sub[0]and a[i+1]==sub[1]and a[i+2]==sub[2]and a[i+3]==sub[3]: 6 print(i,a[i]+a[i+1]+a[i+2]+a[i+3]) 7 i +=1
[root@localhost ~]# python 1.py (6, 'jeap')
切片:stringname[start:end]
例:s='welcome to the cruel world'
t='welcome to ' 将组成新字符串 t=‘welcome to the cruel world’
1 s='welcome to the cruel world' 2 t='welcome to ' 3 i=11 4 while i <len(s): 5 t +=s[i] 6 i +=1 7 print(t)
[root@localhost ~]# python 1.py welcome to the cruel world
切片
1 a='hello the cruel world' 2 print(len(a)) 3 print(a[0]) 4 print(a[3:5]) 5 print(a[3:]) 6 print(a[:21]) 7 print(a[:])
[root@localhost ~]# python 1.py 21 h lo lo the cruel world hello the cruel world hello the cruel world
例:a=‘hello the cruel world’,看字符串a中是否有子串sub=‘rue’
1 a='hello the cruel world' 2 sub='rue' 3 i=0 4 while i<len(a)-len(sub)+1: 5 if a[i:i+3]==sub: 6 print('yes',i) 7 i +=1
[root@localhost ~]# python 1.py ('yes', 11)