Chinaunix首页 | 论坛 | 博客
  • 博客访问: 783639
  • 博文数量: 108
  • 博客积分: 2887
  • 博客等级: 少校
  • 技术积分: 1570
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-28 07:51
文章分类

全部博文(108)

文章存档

2012年(4)

2011年(86)

2010年(18)

分类: Python/Ruby

2011-12-17 16:31:52

Python中没有一个特定的类型来对应字符,幸运的是我们可以使用内建的list方法,用字符串作为参数将其装换为长度为1的字符串来当作字符使用:
>>>Test=list(TestString)
或者可以使用列表推导:
>>>Test=[dosomething_with(c) for c in TestString]
祸使用内建的函数map():
>>>Test=map(dosomething,TestString)

获得字符串所有字符的集合:
>>>import sets
>>>TesetSet=sets.Set('abcdefghigk')
>>>TestSet_1=sets.Set('kdieudooeioiqaacqeodu')
取他们的交集:
>>>Test=''.join(TestSet & TestSet_1)


字符及值之间的转换:
ASCII值:
>>>print ord('a')
97
>>>print chr(97)
a
Unicode值:
>>> print ord(u'\u2011')
8209
>>> print unichr(8209)
>>> print repr(unichr(8209))
u'\u2011'

将一个字符串转换为包含各个字符值的列表:
>>> print map(ord,'linux')
[108, 105, 110, 117, 120]

反过来:
>>> print ''.join(map(chr,range(97,100)))
abc
>>> 
>>> print map(chr,range(97,100))
['a', 'b', 'c']
>>>


测试一个对象是否为类字符串:
用isinstance和basestring,basestring是str和unicode类型的共同基类



字符串对齐:
                    左对齐              右对齐                 居中
>>> print '|','test'.ljust(15),'|','test'.rjust(15),'|','test'.center(15),'|'
| test            |            test |       test      |
>>>
>>>
>>> print '|','test'.ljust(15,'#'),'|','test'.rjust(15,'#'),'|','test'.center(15,'#'),'|'
| test########### | ###########test | ######test##### |
>>> 



去除字符串两端的空格:
>>> test="          text          "
>>> print '|',test.lstrip(),'|','test'.rstrip(),'|','test'.strip(),'|'
| text           | test | test |
>>> 
>>> print '|',test.rstrip(),'|','test'.rstrip(),'|','test'.strip(),'|'
|           text | test | test |
>>> print '|',test.rstrip(),'|','test'.lstrip(),'|','test'.strip(),'|'
|           text | test | test |
>>> (为什么??直接修改了源串??)
>>> 
>>> 
>>> print '|',test.lstrip(),'|'
| text           |
>>> print '|',test.rstrip(),'|'
|           text |
>>> print '|',test.strip(),'|'
| text |
>>> 

带参数的strip():
>>> test="abc          text          xyz"
>>> print '|',test.strip(),'|'
| abc          text          xyz |
>>> 
>>> 
>>> print '|',test.strip('ax'),'|'
| bc          text          xyz |
>>> 
>>> print '|',test.strip('abcxyz'),'|'
|           text           |
>>> 

字符串的合并:
 使用''.join(string)
 使用支付串格式化操作符% print '%s %s'  %(Test1,Test2) 
 使用连接符 '+'

注意:在Pyton中,字符串是不可变对象,任何对字符串的操作(包括拼接)都将产生一个新的字符串对象而不是修改原来的对象,因此,拼接N个字符串将涉及创建并丢弃N-1个中间结果,也有不创建中间结果的方法,但是可能不能一步到位。          
在此推荐使用''.join()方法,性能更佳


字符串逐字符或逐词反转:
逐字符反转:
>>> Test='linux'
>>> ReTest=Test[::-1]
>>> ReTest
'xunil'
逐词反转:
>>> ReTest='I love linux'
>>> 
>>> ReTest
'I love linux'
>>> Retest=ReTest.split()
>>> Retest
['I', 'love', 'linux']
>>> 
>>> Retest.reverse()
>>> Retest=' '.join(Retest)
>>> Retest
'linux love I'
>>> 
逐词反转而不改变原先的空格:
>>> ReTest
'I love linux'
>>> 
>>> import re
>>> test=re.split(r'(\s+)',ReTest)
>>> test.reverse()
>>> test=''.join(test)
>>> test
'linux love I'
>>> 


过滤字符串中不属于指定集合的字符
import string
allchars=string.maketrans('','')
def makefilter(keep):
    delchars=allchars.translate(allchars,keep)
    def thefilter(s):
        return s.translate(allchars,delchars)
    return thefilter
if __name__=='main':
    just_vowels=makefilter('aeiouy')
    print just_vowels('four score and seven years ago')
使用了闭包的,闭包是一次性完成所有准备工作的最好的方法。
此代码只使用与ASCII                                                                                                                                 



                                                                                                                                                                                                                                                                                                                          




阅读(2399) | 评论(0) | 转发(0) |
0

上一篇:openstack_easy_install

下一篇:Open vSwitch 使用

给主人留下些什么吧!~~