基础知识回顾——元组和字符串

元组

元组是一种序列,特点是不能修改,且没有方法,通用序列的操作对元组都适用。

 1 >>> 1,2,3  #创建元组,用逗号分隔
 2 (1, 2, 3)
 3 >>> 42,    
 4 (42,)
 5 >>> 3*(40+2)
 6 126
 7 >>> 3*(40+2,)
 8 (42, 42, 42)
 9 
10 tuple = ('apple','banana','grape','orange')   #元组的打包和解包
11 >>> a,b,c,d = tuple
12 >>> print a,b,c,d
13 apple banana grape orange

 

字符串

字符串是一种特殊的元组,和元组不同的是字符串有方法,通用的序列操作对字符串同样适用。 

字符串方法:

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

1 >>> string = "python is not python"
2 >>> string.find('python')
3 0
4 >>> string.find('is')
5 7
6 >>> string.find('me')
7 -1

 

2)join:连接序列中的元素

 1 >>> dirs = ' ','users','bin'
 2 >>> '/'.join(dirs)
 3 ' /users/bin'
 4 
 5 >>> seq1 = ['1','2','3','4','5']    
 6 >>> '-' .join(seq1)
 7 '1-2-3-4-5'

 

3)lower:返回字符串的小写字母版(其他版本capitalize、upper、title)

1 >>> strings ="hello PYTHON"
2 >>> strings.lower()
3 'hello python'
4 >>> strings.upper()
5 'HELLO PYTHON'
6 >>> strings.title()
7 'Hello Python'
8 >>> strings.capitalize()
9 'Hello python'

 

4)replace:返回某个字符串的所有匹配项均被替换之后的字符串

1 >>> 'this is a test'.replace('is','eez')
2 'theez eez a test'

 

5)split:join的逆方法,用来将字符串分割成序列

1 >>> ' /user/bin'.split('/')
2 ['', 'user', 'bin']
3 >>> 'useing python and c'.split()    #如果不提供分隔符,程序会把所有的空格作为分隔符
4 ['useing', 'python', 'and', 'c']

 

6)strip:返回去除两侧(不包括内部)空格的字符串

1 >>> title = " !+ happy days !+  "  #前无空格,后有空格
2 >>> title.strip()
3 '!+ happy days !+'
4 >>> title.strip('!+')    #'!+'无空格
5 ' !+ happy days !+  '
6 >>> title.strip(' !+ ')  #'!+'有空格
7 'happy days'

 

tip:可以通过help()查看说明文档

>>> help(tuple)
Help on class tuple in module __builtin__:

class tuple(object)
 |  tuple() -> empty tuple
 |  tuple(iterable) -> tuple initialized from iterable's items
 |  
 |  If the argument is a tuple, the return value is the same object.
 |  
 |  Methods defined here:
 |  
 |  __add__(...)
 |      x.__add__(y) <==> x+y
 |  
 |  __contains__(...)
 |      x.__contains__(y) <==> y in x
 |  
 |  __eq__(...)
 |      x.__eq__(y) <==> x==y
 |  
 |  __ge__(...)
 |      x.__ge__(y) <==> x>=y
 |  
 |  __getattribute__(...)
 |      x.__getattribute__('name') <==> x.name
 |  
 |  __getitem__(...)
 |      x.__getitem__(y) <==> x[y]
 |  
 |  __getnewargs__(...)
 |  
 |  __getslice__(...)
 |      x.__getslice__(i, j) <==> x[i:j]
 |      
 |      Use of negative indices is not supported.
 |  
 |  __gt__(...)
 |      x.__gt__(y) <==> x>y
 |  
 |  __hash__(...)
 |      x.__hash__() <==> hash(x)
 |  
 |  __iter__(...)
 |      x.__iter__() <==> iter(x)
 |  
 |  __le__(...)
 |      x.__le__(y) <==> x<=y
 |  
 |  __len__(...)
 |      x.__len__() <==> len(x)
 |  
 |  __lt__(...)
 |      x.__lt__(y) <==> x<y
 |  
 |  __mul__(...)
 |      x.__mul__(n) <==> x*n
 |  
 |  __ne__(...)
 |      x.__ne__(y) <==> x!=y
 |  
 |  __repr__(...)
 |      x.__repr__() <==> repr(x)
 |  
 |  __rmul__(...)
 |      x.__rmul__(n) <==> n*x
 |  
 |  count(...)
 |      T.count(value) -> integer -- return number of occurrences of value
 |  
 |  index(...)
 |      T.index(value, [start, [stop]]) -> integer -- return first index of value.
 |      Raises ValueError if the value is not present.
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __new__ = <built-in method __new__ of type object>
 |      T.__new__(S, ...) -> a new object with type S, a subtype of T

 

总结:元组不可变,没有方法

   字符串特殊元组,不可变,有find()、join()、replace()、split()、strip()等方法

 

posted on 2017-01-14 11:54  Ryana  阅读(418)  评论(0编辑  收藏  举报