zoukankan      html  css  js  c++  java
  • 基础知识回顾——元组和字符串

    元组

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

     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()等方法

  • 相关阅读:
    打造基于CentOS7的xfce最简工作环境
    Linux下C程序的编辑,编译和运行以及调试
    修正 XE6 TListView 上方 SearchBok 右边的清除钮显示
    TabControl 显示彩色的图示 (XE6 Firemonkey)
    TSwitch 中文简繁显示支持(XE6 Android)
    改变 TMemo 的背景颜色 (Firemonkey)
    修正 XE5 Android 键盘三个问题
    Delphi 收藏
    展示 Popup 的使用方法
    ListView 下拉更新 (支持 Android)
  • 原文地址:https://www.cnblogs.com/Ryana/p/5967905.html
Copyright © 2011-2022 走看看