python 学习笔记 3

ch3 使用字符串 

* format string ==> print format % values

* 当format有多个参数时,需要括号

  ‘%s + %s = %s' % (1,100,101) 

* template:  

   s=string.Template('A $v0 must never${v1}')

   input={'v0':'dog','v1':'bite'}

    print(s.substitute(input))

    or print(s.substitute(v0='dog',v1='bite'))

*  字符串查找 “this is some".find('is') ==> 2 (index=2)

*  s.find(str[,start, end])

*  其他方法:

    '+'.join(['1','2','3'])==>'1+2+3'

    'ABC'.lower() ==> 'abc'

    "This is xx".replace('xx','yy')

    '1+2+3'.split('+')==>['1','2','3']

    '   abc  '.strip() ==> 'abc'

*******************************************************************

ch4.  dictionary

*****************

*  define a dictionary

  {'alice':'2341','beth':'9102', 'cecil':'3528'}

*  dict() function

  items=[('name','gumby'),('age',42)]

  d=dict(items)

  or dict(name='gumby', age=42) 

*   基本字典操作

  # len(d)

  # d[k]

  # d[k]=y

  # del d[k]

  # k in d

  # d.clear()

  # 浅复制 y=x.copy()

  # 深复制 y=copy.deepcopy(d) # copy is a package name, should use import copy

  # {}.fromkeys(['name', 'age']) ==> {'name':None, 'age':None}

  # d.get('name') # 更宽松的访问方法

  # d.items() ==> 将字典转成列表

  # d.keys(), d.iterkeys()# iterkeys not work on 3.0

  # d.pop('x')

  # d.popitem()

  # d.setdefault('name', 'n/a')

  # d.update(d2) # using dictionary d2 to update d

  # d.values

 

 

 

posted on 2012-02-25 13:43  learner  阅读(159)  评论(0)    收藏  举报

导航