python学习笔记(三)字符串

来源:互联网 发布:怎么查淘宝等级手机 编辑:程序博客网 时间:2024/05/17 07:57

字符串的一些重要方法方法

find(),返回子串所在位置的最左端索引,若找不到则返回-1。

join(),是split()方法的逆方法,用来在队列中添加元素。

seq = ['1', '2', '3']sep = '/'print sep.join(seq)sep = '\\'print sep.join(seq)运行结果:

运行结果:

1/2/3 

1\2\3

 

lower(),返回字符串的小写字母版。

replace(string a,string b),返回某字符串a的所有匹配项均被替换成b之后得到的字符串。

split(),将字符串分割成序列。

strip(),去除两侧(不包括内部)的字符串。

translate(),同时进行多个单个字符的替换。

 

以下是测试程序

# -*- coding:utf-8 -*-'''Created on 2013-7-7@author: GinSmile'''from string import maketransmyStr = '   !This is a simple.  !! 'myStr = myStr.strip(' !') #去除两边空格和叹号print myStr  #This is a simple.print myStr.lower()  #this is a simple.print myStr.capitalize()  #This is a simple.print myStr.upper()  #THIS IS A SIMPLE.seq = myStr.split(' ')print seq  #['this', 'is', 'a', 'simple.']print ' '.join(seq) #this is a simple.table = maketrans('cs', 'kz')print len(table)  #256print table[97:123]  #abkdefghijklmnopqrztuvwxyz#以上,table作为转换表编写成功print 'this is another simple. cut the hair~~'.translate(table)  #thiz iz another zimple. kut the hair~~


原创粉丝点击