【Python】 string

来源:互联网 发布:php 工作流 编辑:程序博客网 时间:2024/05/17 13:43
string: 字符串(即不能修改的字符list)
str = “Hello My friend”
字符串是一个整 体。如果你想直接修改字符串的某一部分,是不可能的。但我们能够读出字符串的某一部分。
子字符串的提取
str[:6]
字符串包含 判断操作符:in,not in
“He” in str
“she” not in str

string模块,还提供了很多方法,如
S.find(substring, [start [,end]]) #可指范围查找子串,返回索引值,否则返回-1
S.rfind(substring,[start [,end]]) #反向查找
S.index(substring,[start [,end]]) #同find,只是找不到产生ValueError异常
S.rindex(substring,[start [,end]])#同上反向查找
S.count(substring,[start [,end]]) #返回找到子串的个数

S.lowercase()
S.capitalize() #首字母大写
S.lower() #转小写
S.upper() #转大写
S.swapcase() #大小写互换

S.split(str, ‘ ‘) #将string转list,以空格切分
S.join(list, ‘ ‘) #将list转string,以空格连接

处理字符串的内置函数
len(str) #串长度
cmp(“my friend”, str) #字符串比较。第一个大,返回1
max(‘abcxyz’) #寻找字符串中最大的字符
min(‘abcxyz’) #寻找字符串中最小的字符

string的转换

oat(str) #变成浮点数,float(“1e-1″) 结果为0.1
int(str) #变成整型, int(“12″) 结果为12
int(str,base) #变成base进制整型数,int(“11″,2) 结果为2
long(str) #变成长整型,
long(str,base) #变成base进制长整型,

字符串的格式化(注意其转义字符,大多如C语言的,略)

str_format % (参数列表) 

String模块中的常量:

string.digits:数字0~9

string.letters:所有字母(大小写)

string.lowercase:所有小写字母

string.printable:可打印字符的字符串

string.punctuation:所有标点

string.uppercase:所有大写字母

[html] view plaincopy
  1. >>> import string  
  2. >>> string.digits  
  3. '0123456789'  
  4. >>> string.letters  
  5. 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'  
  6. >>> string.lowercase  
  7. 'abcdefghijklmnopqrstuvwxyz'  
  8. >>> string.printable  
  9. '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'  
  10. >>> string.punctuation  
  11. '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'  
  12. >>> string.uppercase  
  13. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'  


1、find函数

在一个较长的字符串中查询子字符串,返回子串所在位置最左端索引,没有找到返回-1

[html] view plaincopy
  1. >>> title = "Monty Python's Flying Circus"  
  2. >>> title.find('Monty')  
  3. 0  
  4. >>> title.find('monty')  
  5. -1  
可以选择起始点和结束点

[html] view plaincopy
  1. >>> title.find('Python')  
  2. 6  
  3. >>> title.find('Python', 3)  
  4. 6  
  5. >>> title.find('Python', 3, 10)  
  6. -1  

2、join函数

在队列中添加元素(只能操作于字符串,返回一个修改后的字符串,但是原字符串不改变)

[html] view plaincopy
  1. >>> seq = ['1', '2', '3', '4', '5']  
  2. >>> sep = '+'  
  3. >>> sep.join(seq)  
  4. '1+2+3+4+5'  
  5. >>> seq  
  6. ['1', '2', '3', '4', '5']  
  7. >>> dirs = '', 'usr', 'bin', 'env'  
  8. >>> '/'.join(dirs)  
  9. '/usr/bin/env'  
  10. >>> print 'C:' + '\\'.join(dirs)  
  11. C:\usr\bin\env  

逆方法:split函数

将字符串分割成序列,返回该序列,原字符串不改变

[html] view plaincopy
  1. >>> word = '1+2+3+4+5'  
  2. >>> word.split('+')  
  3. ['1', '2', '3', '4', '5']  
  4. >>> word  
  5. '1+2+3+4+5'  

3、lower函数

返回字符串的小写字母版

[html] view plaincopy
  1. >>> 'fafDAWdfaweDWED'.lower()  
  2. 'fafdawdfawedwed'  
扩展:

title函数:首字母大写,其他小写

[html] view plaincopy
  1. >>> "that's all folks".title()  
  2. "That'S All Folks"  
capwords函数:功能同上,为string模块中函数

[html] view plaincopy
  1. >>> import string  
  2. >>> string.capwords("that's all folks")  
  3. "That's All Folks"  

4、replace函数

返回某字符串所有匹配项均被替换之后得到的字符串,原字符串不改变

[html] view plaincopy
  1. >>> word = 'this is a test'  
  2. >>> word.replace('is', 'eez')  
  3. 'theez eez a test'  
  4. >>> word  
  5. 'this is a test'  

maketrans函数:功能同上,string中的转换表,共有256个项目,函数接受2个等长的字符串,第一个字符串中的每个字符都用第二个字符串中相应位置的字符来进行替换

maketrans类似于一种规则,经常与translate结合,以完成一些普通函数无法完成的字符串替换

[html] view plaincopy
  1. >>> from string import maketrans  
  2. >>> table = maketrans('cs', 'kz')  
  3. >>> len(table)  
  4. 256  
  5. >>> table[97:123]  
  6. 'abkdefghijklmnopqrztuvwxyz'  
  7. >>> maketrans('','')[97:123]  
  8. 'abcdefghijklmnopqrstuvwxyz'  

translate函数:功能同上,但是只能处理单个字符,有2个参数,第一个是替换,第二个是删除

例:table承继maketrans中的table

[html] view plaincopy
  1. >>> 'this is an incredible test'.translate(table)  
  2. 'thiz iz an inkredible tezt'  
  3. >>> 'this is an incredible test'.translate(table, ' ')  
  4. 'thizizaninkredibletezt'  


5、strip函数

去除两侧(不包括内部)空格的字符串,原序列不变

[html] view plaincopy
  1. >>> word = '   this is test    '  
  2. >>> word.strip()  
  3. 'this is test'  
  4. >>> word  
  5. '   this is test    '  
可在strip()加入参数,以去除想要去掉的指定字符

[html] view plaincopy
  1. >>> '***  SPAM  *  for  *  everyone!!!  ***'.strip('*')  
  2. '  SPAM  *  for  *  everyone!!!  '  
  3. >>> '***  SPAM  *  for  *  everyone!!!  ***'.strip('* ')  
  4. 'SPAM  *  for  *  everyone!!!'  
  5. >>> '***  SPAM  *  for  *  everyone!!!  ***'.str

0 0
原创粉丝点击