Python基础篇(三)

     元组是序列的一种,与列表的区别是,元组是不能修改的。

     元组一般是用圆括号括起来进行定义,如下:

     >>> (1,2,3)[1:2]
     (2,)

     如果元组中只有一个元素,元组的表示有些奇怪,末尾需要加上一个逗号:

     >>> (1,2,3)[1:2]
     (2,)

   

      >>> 3*(3)
      9
      >>> 3*(3,)
      (3, 3, 3)

      tuple函数

      tuple函数用于将任意类型的序列转换为元组:

      >>> tuple([1,2,3])
      (1, 2, 3)
      >>> tuple("123")
      ('1', '2', '3')

      元组有以下2方面的用途:

      第一,元组可以在映射中当键来使用,列表则不行。

      第二,元组可以作为很多内建函数和方法的返回值。

 

      字符串作为序列的一种,序列的共有操作,如索引,分片,乘法等字符串也都是支持的。

     >>> website = "http://www.python.org"
     >>> website[-3::-1]
     'o.nohtyp.www//:ptth'
     >>> website[-3:]
     'org'

     下面是字符串所特有的一些操作。

     字符串格式化使用:

     >>> format ="hello %s,%s is good!"
     >>> values =('world','world')
     >>> print(format % values)
     hello world,world is good!

     参数(values)可以使用字符串,数字,元组或者字典来进行表示。

    >>> format ="PI with three decimials : %.3f"
    >>> from math import pi
    >>> print(format % pi)
    PI with three decimials : 3.142

    元组作为表达式的一部分出现时一定要加上括号:

    >>> "%s plus %s equas %s" % (1,2,2)
    '1 plus 2 equas 2'

   

     以下是转换符的一些较为深入的使用方法:

     转换标志:-表示左对齐,+表示要加上正负号,""表示正数之前加上空格(常用于对齐),0表示数字位数不足则用0填充

     >>> '%-10.2f' %pi
     '3.14      '
     >>> '%10.2f' %pi #正常情况是右对齐
     '      3.14'     

     >>> print("%+5d" % 10 + "\n"+ "%5d" % -10) #+表示要加上正负号
     +10
     -10

    >>> print("% 5d" % 10 + "\n"+ "% 5d" % -10)  #""表示正数之前加上空格
      10
    -10     

    >>> print("%010.2f" % pi)  #0表示数字位数不足则用0填充,共10位,2位小数
    0000003.14

    

     转换类型:

     d,i         带符号的十进制

     u            不带符号的十进制

     o            不带符号的八进制

     x            不带符号的十六进制(小写)         X            不带符号的十六进制(大写)

     e            科学计数法(小写)                    E             科学计数法(大写) 

     f,F        浮点数

     r,s        字符串

     

    find函数

    find函数用于查找子字符串首次出现的位置,用index函数可以完成同样功能

    >>> "a little girl".find("little")

    2   

   >>> "a little girl".index("little",1)  #1表示其实位置,还可以加上搜索的结束位置
   2 

   >>> "a little girl".find("little",1,3)
   -1

   

    join函数 

    join函数的作用和split函数相反,用于向字符串中增加元素 

    >>> seq = ['ab','cd','ef']
    >>> sign = ','
    >>> sign.join(seq)
    'ab,cd,ef'

 

    lower函数

    lower函数用于将一个字符串中的字符全部转换为小写,islower可以判断字符串是否全是小写

    >>> "A Good Language".lower()
    'a good language'
    >>> "A Good Language".upper()
    'A GOOD LANGUAGE'

    >>> "A Good Language".islower()

    False

    title()函数可以将字符串中每个单词的首字母改为大写

    >>> "a good language".title()
    'A Good Language'
   

     replace函数

     replace函数用于替换字符串中的子字符串

     >>> "to be or not to be".replace("to","will")
     'will be or not will be'

 

      split函数

      split函数用于将字符串按指定字符划分为序列

      >>> "usr/bin/env".split("/")
      ['usr', 'bin', 'env']

 

      strip函数

      strip函数可以去除字符串首尾处的空格(可以是多个),类似于java的trim方法

      >>> names =['jim','tom','tim']
      >>> name = "tom "
      >>> if name.strip() in names : print("found it")
      ...
      found it

 

      translate函数

      translate函数配合string内置的maketrans函数可以完成字符替换的功能,例如下面将'c'替换为'k','s'替换为'z':

      >>> str = "this is an incredible test"
      >>> t = str.maketrans('cs','kz')
      >>> str.translate(t)
      'thiz iz an inkredible tezt'

 

     

posted on 2014-07-22 17:33  lnlvinso  阅读(280)  评论(0编辑  收藏  举报