Python学习笔记(三)字符串
2013-07-07 21:43
441 查看
字符串的一些重要方法方法
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 maketrans myStr = ' !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) #256 print table[97:123] #abkdefghijklmnopqrztuvwxyz #以上,table作为转换表编写成功 print 'this is another simple. cut the hair~~'.translate(table) #thiz iz another zimple. kut the hair~~
相关文章推荐
- Python学习笔记-第三章 使用字符串
- Python学习笔记(三)英文字符串的大小写转换
- python 学习笔记(4)解析和字符串处理
- Python 【字符串】 学习笔记
- Python核心编程--学习笔记--6--序列(上)字符串
- python基础教程学习笔记 第三章 字符串
- Python学习笔记 - 字符串的方法
- Python学习笔记 - 字符串和编码
- python2学习笔记 第三章 使用字符串
- Python学习笔记-长字符串
- python cookbook 学习笔记 -- 1.7 将字符串逐字符或逐词反转
- Python学习笔记五:数字和字符串
- python学习手册笔记--第7章--字符串
- Python 学习笔记-字符串
- Python学习笔记5-字符串、bool、数值操作和数组字典排序
- Python学习笔记-Python的字符串,格式化,条件判断,循环、raw_input的注意事项
- Python 学习笔记 - 字符串的格式化
- Python学习笔记(六)数字与字符串…
- 谭子python学习笔记--字符串
- Python学习笔记(四):字符串的学习