Python笔记004——字符串
2015-11-12 23:32
856 查看
字符串基础
1 字符串定义:用引号引起来的字符集合称之为字符串(单引号,双引号,三双引号,三单引号)
三引号(三单或三双)支持多行或也可表示注释,而单双引只能一行(但是可以加\n)
2 转义字符串:
常见有 \n 回车字符,
\t 下一制表位
\'' 双引号
\' 单引号
\\ 输出斜杠
\b 往前退格(删除前面那个字符)
3 原字符串,若不要被看成是转义字符,那么需在字符串前加上r,称此时的字符串为原字符串
字符串运算
加法运算(拼接)
乘法运算(重复)
关系运算(比较:对应字符相比)
in 运算与not in运算 (a in b 表示判断a是不是在b里面)
字符串访问(index索引【0到len()-1】,slice)
slice切片:str_name[start:end:step]
统计字符串个数及其位置
字符串函数【help(str)】
常见函数有find,index,replace,count,split,strip
isspace()
find()和rfind()
基于前一次位置往后找
例:
1 字符串定义:用引号引起来的字符集合称之为字符串(单引号,双引号,三双引号,三单引号)
三引号(三单或三双)支持多行或也可表示注释,而单双引只能一行(但是可以加\n)
2 转义字符串:
常见有 \n 回车字符,
\t 下一制表位
\'' 双引号
\' 单引号
\\ 输出斜杠
\b 往前退格(删除前面那个字符)
print('hello world') print('hello world\bwujiadong')#删除前一个字符,后面字符往前进一个
[root@localhost ~]# python 1.py hello world hello worlwujiadong
3 原字符串,若不要被看成是转义字符,那么需在字符串前加上r,称此时的字符串为原字符串
字符串运算
加法运算(拼接)
a='wu' b='jia' c='dong' print(a+b+c)
[root@localhost ~]# python 1.py wujiadong
乘法运算(重复)
a='wu' b='jia' c='dong' print(a*3)
wuwuwu
关系运算(比较:对应字符相比)
a='wu' b='jia' c='dong' if a>b: print(a) else: print(b)
[root@localhost ~]# python 1.py wu
in 运算与not in运算 (a in b 表示判断a是不是在b里面)
a='wujiadong' b='wu' if 'wu' in a: #or b ,not wu print('true') else: print('false')
[root@localhost ~]# python 1.py true
字符串访问(index索引【0到len()-1】,slice)
a='wujiadong'# 0,1,2,3,4,5,6,7,8 print(a[0]) print(a[8]) print(a[0]+a[8]) i=0 while i<len(a): print(a[i]) i +=1
[root@localhost ~]# python 1.py w g wg w u j i a d o n g
a='wujiadong'# 0,1,2,3,4,5,6,7,8 i=0 while i< len(a): #为什么这里不能 <= if a[i]=='d': #遍历的方式找一个字符 print(a[i],i) i=i+1
[root@localhost ~]# python 1.py ('d', 5)
slice切片:str_name[start:end:step]
统计字符串个数及其位置
a='wujiadong'*17 b='j' print(a.count(b))
oot@localhost ~]# python 1.py 17
a='wujiadong'*17 b='j' #print(a.count(b)) i=0 while i< len(a): if a[i]==b: print(i,a[i]) 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()
s=' wujiadong ' i=0 while i<len(s): if s[i].isspace(): # print(i) i += 1
[root@localhost ~]# python 1.py 0 10
find()和rfind()
sub='11aa22cc33aa' a=sub.find('aa',0,12) #找到的第一个符合字符的index b=sub.rfind('aa',0,12) #找到最后一个符合的字符的index print(a,b)
[root@localhost ~]# python 1.py (2, 10)
基于前一次位置往后找
s= 'hello jeapedu.com'*4 sub='eape' i=0 pos=-4 #pos= -len(sub) while i<s.count('eape'): pos=s.find(sub,pos+len(sub)) print(pos) i += 1
[root@localhost ~]# python 1.py 7 24 41 58
例:
s='http://www.baidu.com http://www.hao123.com http://www.sohu.com http://www.yahu.com' h='http' c='.com' posh=-len(h) posc=-len(c) i=0 while i<s.count(h): posh= s.find(h,posh+len(h))#posh= s.find(h,posc+len(h)) posc= s.find(c,posh+len(c))#posc= s.find(c,posh+len(c)) print(posh), print(posc), print(s[posh:posc+len(c)]) 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[/code]
去掉字符串s前后的空格s=' wujiadong ' i=0 h=-1 e=-1 while i<len(s): if not s[i].isspace(): h=i break i += 1 print(h) i=len(s)-1 while i>=0: # 从最后一个往第一个遍历 if not s[i].isspace(): e=i break i -=1 print(e) print(s[h:e+1])[root@localhost ~]# python 1.py 1 9 wujiadong
strip函数a=' wujiadong ' t="|"+a+'|' print('1',t) t1='|'+a.strip()+'|' print('2',t1) b='ab123456ab' t1=b.strip('ab') print('3',t1) t2=b.strip('a') print('4',t2) b2='123xxx321' t3=b2.strip('321') print('5',t3)[root@localhost ~]# python 1.py ('1', '| wujiadong |') ('2', '|wujiadong|') ('3', '123456') ('4', 'b123456ab') ('5', 'xxx')
例:
将字符串s中的网址提出来s='*&)%$#(@http://www.baidu.com*&^)^(%$#' a='*&^)(%$#@' i=0 h=0 while i<len(s): if s[i] not in a: h=i break i +=1 s=s[i:] print(s) i=len(s)-1 e=0 while i>=0: if s[i] not in a: e=i+1 break i -=1 s=s[:e] print(s)[root@localhost ~]# python 1.py http://www.baidu.com*&^)^(%$# http://www.baidu.com
字符串的应用
例:用random.randint随机产生一个六位数
1)判断此数里是否含有4和7(都有)import random i=1 while i<=10: x=random.randint(100000,1000000) s=str(x) if '4'in s and '7' in s: print(s,'has 4 and 7') 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(都有)import random i=1 while i<=10: x=random.randint(100000,1000000) s=str(x) if '4'not in s and '7' not in s: #print(s,'has 4 and 7') if '6' in s and '8' in s: print(s,'no 4and7,but have 6and8') i +=1[root@localhost ~]# python 1.py ('181196', 'no 4and7,but have 6and8') ('858986', 'no 4and7,but have 6and8')
3)若有4和7出现,打印其出现的位置import random i=1 while i<=10: x=random.randint(100000,1000000) s=str(x) if '4' in s or '7' in s: print(s) k=0 while k < len(s): if s[k]=='4'or s[k]=='7': print(k,s[k]) k +=1 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-jeapedu100i=0 while i <=100: if i<10: print('jeapedu00'+str(i)) elif i<100: print('jeapedu0'+str(i)) else: print('jeapedu'+str(i)) i +=1
例:构造a01b02c03.....y25z26
利用chr(ascii转字符)ord(字符转ascii)int(字符串转整型)实现翻译a=chr(65) print(a) a=chr(97) print(a) b=ord('a') print(b) b=ord('A') print(b)a=97 i=0 while i< 26: # print(chr(a+i)) print(chr(a+i)+str(i)) i +=1
如何判断字符串里有‘jeap’这个子字符串?其位置?a= 'hello jeapedu.com' sub='jeap' i=0 while i<len(a): if a[i]==sub[0]and a[i+1]==sub[1]and a[i+2]==sub[2]and a[i+3]==sub[3]: print(i,a[i]+a[i+1]+a[i+2]+a[i+3]) 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’s='welcome to the cruel world' t='welcome to ' i=11 while i <len(s): t +=s[i] i +=1 print(t)[root@localhost ~]# python 1.py welcome to the cruel world
切片a='hello the cruel world' print(len(a)) print(a[0]) print(a[3:5]) print(a[3:]) print(a[:21]) 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’a='hello the cruel world' sub='rue' i=0 while i<len(a)-len(sub)+1: if a[i:i+3]==sub: print('yes',i) i +=1[root@localhost ~]# python 1.py ('yes', 11)
相关文章推荐
- Ubuntu14.04下安装Python2.7+Theano0.7+CUDA7.5
- Odoo8 custom view module
- python 3, 字符串连接速度
- Python模块
- Python篇----基础知识(供查询)
- Python遍历删除元素
- 第一节 个人博客搭建概览
- ipython 正确的打开方式
- python 文件、目录操作(新增、移动、删除等)
- webpy源码阅读
- python,学校成员类的例子,老师和学生(python class父类与子类之间的联系与逻辑)
- 自学Python第一周——print的用法总结
- 用Python读取大文件(上)
- Eclipse配置Spark Python开发环境
- Python学习分享
- (转)优秀的 python 机器学习库
- Python入门import问题
- 使用python进行数据分析
- Python-第一天
- Eclipse安装python插件