数据类型方法

字符串是一个类,"hello world"是它的对象

常用方法:

  • 移除空白
  • 分割
  • 长度
  • 索引
  • 切片

class str(basestring):

    def capitalize(self):
        """ 首字母变大写 """
             (返回副本)

    def center(self, width, fillchar=None):
        """ 内容居中,width:总长度;fillchar:空白处填充内容,默认无 """

    def count(self, sub, start=None, end=None):
        """ 子序列个数 """

    def decode(self, encoding=None, errors=None):
        """ 解码 """

    def encode(self, encoding=None, errors=None):
        """ 编码,针对unicode """

    def endswith(self, suffix, start=None, end=None):
        """ 是否以 xxx 结束 """

    def expandtabs(self, tabsize=None):
        """ 将tab转换成空格,默认一个tab转换成8个空格 """
                    (返回副本)

    def find(self, sub, start=None, end=None):
        """ 寻找子序列位置,如果没找到,返回 -1 """

    def format(*args, **kwargs): # known special case of str.format
        """ 字符串格式化,动态参数,将函数式编程时细说 """

    def index(self, sub, start=None, end=None):
        """ 子序列位置,如果没找到,报错 """

    def isalnum(self):
        """ 是否是字母和数字 """

    def isalpha(self):
        """ 是否是字母 """

    def isdigit(self):
        """ 是否是数字 """

    def islower(self):
        """ 是否小写 """

    def isspace(self):  

    def istitle(self):  

    def isupper(self):  

    def join(self, iterable):
        """ 连接 """

    def ljust(self, width, fillchar=None):
        """ 内容左对齐,右侧填充 """

    def lower(self):
        """ 变小写 """
                   (返回副本)

    def lstrip(self, chars=None):
        """ 移除左侧空白 """
                  (返回副本)  

    def partition(self, sep):
        """ 分割,前,中,后三部分 """

    def replace(self, old, new, count=None):
        """ 替换 """
                 (返回副本)  

    def rfind(self, sub, start=None, end=None):  

    def rindex(self, sub, start=None, end=None):  

    def rjust(self, width, fillchar=None):  

    def rpartition(self, sep):  

    def rsplit(self, sep=None, maxsplit=None):  

    def rstrip(self, chars=None):
                   (返回副本)

    def split(self, sep=None, maxsplit=None):
        """ 分割, maxsplit最多分割几次 """

    def splitlines(self, keepends=False):
        """ 根据换行分割 """

    def startswith(self, prefix, start=None, end=None):
        """ 是否起始 """

    def strip(self, chars=None):
        """ 移除两端空白 """
                  (返回副本)

    def swapcase(self):
        """ 大写变小写,小写变大写 """

    def title(self):  

    def translate(self, table, deletechars=None):
        """
        转换,需要先做一个对应表,最后一个表示删除字符集合
        intab = "aeiou"
        outtab = "12345"
        trantab = maketrans(intab, outtab)
        str = "this is string example....wow!!!"
        print str.translate(trantab, ‘xm‘)
        """
              (返回副本)   

    def upper(self):
                   (返回副本)
    def zfill(self, width):
        """方法返回指定长度的字符串,原字符串右对齐,前面填充0。"""

    def _formatter_field_name_split(self, *args, **kwargs): # real signature unknown
        pass

    def _formatter_parser(self, *args, **kwargs): # real signature unknown
        pass

    def __add__(self, y):
        """ x.__add__(y) <==> x+y """
        pass

    def __contains__(self, y):
        """ x.__contains__(y) <==> y in x """
        pass

    def __eq__(self, y):
        """ x.__eq__(y) <==> x==y """
        pass

    def __format__(self, format_spec):
        """
        S.__format__(format_spec) -> string

        Return a formatted version of S as described by format_spec.
        """
        return ""

    def __getattribute__(self, name):
        """ x.__getattribute__(‘name‘) <==> x.name """
        pass

    def __getitem__(self, y):
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __getnewargs__(self, *args, **kwargs): # real signature unknown
        pass

    def __getslice__(self, i, j):
        """
        x.__getslice__(i, j) <==> x[i:j]

                   Use of negative indices is not supported.
        """
        pass

    def __ge__(self, y):
        """ x.__ge__(y) <==> x>=y """
        pass

    def __gt__(self, y):
        """ x.__gt__(y) <==> x>y """
        pass

    def __hash__(self):
        """ x.__hash__() <==> hash(x) """
        pass

    def __init__(self, string=‘‘): # known special case of str.__init__
        """
        str(object=‘‘) -> string

        Return a nice string representation of the object.
        If the argument is a string, the return value is the same object.
        # (copied from class doc)
        """
        pass

    def __len__(self):
        """ x.__len__() <==> len(x) """
        pass

    def __le__(self, y):
        """ x.__le__(y) <==> x<=y """
        pass

    def __lt__(self, y):
        """ x.__lt__(y) <==> x<y """
        pass

    def __mod__(self, y):
        """ x.__mod__(y) <==> x%y """
        pass

    def __mul__(self, n):
        """ x.__mul__(n) <==> x*n """
        pass

    @staticmethod # known case of __new__
    def __new__(S, *more):
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

    def __ne__(self, y):
        """ x.__ne__(y) <==> x!=y """
        pass

    def __repr__(self):
        """ x.__repr__() <==> repr(x) """
        pass

    def __rmod__(self, y):
        """ x.__rmod__(y) <==> y%x """
        pass

    def __rmul__(self, n):
        """ x.__rmul__(n) <==> n*x """
        pass

    def __sizeof__(self):
        """ S.__sizeof__() -> size of S in memory, in bytes """
        pass

    def __str__(self):
        """ x.__str__() <==> str(x) """
        pass

str

str方法

list方法:

基本操作:

  • 索引
  • 切片
  • 追加
  • 删除
  • 长度
  • 切片
  • 循环
  • 包含

class list(object):
    """
    list() -> new empty list
    list(iterable) -> new list initialized from iterable‘s items
    """
    def append(self, p_object): # real signature unknown; restored from __doc__
        """ L.append(object) -- append object to end """
        pass

    def count(self, value): # real signature unknown; restored from __doc__
        """ L.count(value) -> integer -- return number of occurrences of value """
        return 0

    def extend(self, iterable): # real signature unknown; restored from __doc__
        """ L.extend(iterable) -- extend list by appending elements from the iterable """
        pass

    def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
        """
        L.index(value, [start, [stop]]) -> integer -- return first index of value.
        Raises ValueError if the value is not present.
        """
        return 0

    def insert(self, index, p_object): # real signature unknown; restored from __doc__
        """ L.insert(index, object) -- insert object before index """
        pass

    def pop(self, index=None): # real signature unknown; restored from __doc__
        """
        L.pop([index]) -> item -- remove and return item at index (default last).
        Raises IndexError if list is empty or index is out of range.
        """
        pass

    def remove(self, value): # real signature unknown; restored from __doc__
        """
        L.remove(value) -- remove first occurrence of value.
        Raises ValueError if the value is not present.
        """
        pass

    def reverse(self): # real signature unknown; restored from __doc__
        """ L.reverse() -- reverse *IN PLACE* """
        pass

    def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
        """
        L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
        cmp(x, y) -> -1, 0, 1
        """
        pass

    def __add__(self, y): # real signature unknown; restored from __doc__
        """ x.__add__(y) <==> x+y """
        pass

    def __contains__(self, y): # real signature unknown; restored from __doc__
        """ x.__contains__(y) <==> y in x """
        pass

    def __delitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__delitem__(y) <==> del x[y] """
        pass

    def __delslice__(self, i, j): # real signature unknown; restored from __doc__
        """
        x.__delslice__(i, j) <==> del x[i:j]

                   Use of negative indices is not supported.
        """
        pass

    def __eq__(self, y): # real signature unknown; restored from __doc__
        """ x.__eq__(y) <==> x==y """
        pass

    def __getattribute__(self, name): # real signature unknown; restored from __doc__
        """ x.__getattribute__(‘name‘) <==> x.name """
        pass

    def __getitem__(self, y): # real signature unknown; restored from __doc__
        """ x.__getitem__(y) <==> x[y] """
        pass

    def __getslice__(self, i, j): # real signature unknown; restored from __doc__
        """
        x.__getslice__(i, j) <==> x[i:j]

                   Use of negative indices is not supported.
        """
        pass

    def __ge__(self, y): # real signature unknown; restored from __doc__
        """ x.__ge__(y) <==> x>=y """
        pass

    def __gt__(self, y): # real signature unknown; restored from __doc__
        """ x.__gt__(y) <==> x>y """
        pass

    def __iadd__(self, y): # real signature unknown; restored from __doc__
        """ x.__iadd__(y) <==> x+=y """
        pass

    def __imul__(self, y): # real signature unknown; restored from __doc__
        """ x.__imul__(y) <==> x*=y """
        pass

    def __init__(self, seq=()): # known special case of list.__init__
        """
        list() -> new empty list
        list(iterable) -> new list initialized from iterable‘s items
        # (copied from class doc)
        """
        pass

    def __iter__(self): # real signature unknown; restored from __doc__
        """ x.__iter__() <==> iter(x) """
        pass

    def __len__(self): # real signature unknown; restored from __doc__
        """ x.__len__() <==> len(x) """
        pass

    def __le__(self, y): # real signature unknown; restored from __doc__
        """ x.__le__(y) <==> x<=y """
        pass

    def __lt__(self, y): # real signature unknown; restored from __doc__
        """ x.__lt__(y) <==> x<y """
        pass

    def __mul__(self, n): # real signature unknown; restored from __doc__
        """ x.__mul__(n) <==> x*n """
        pass

    @staticmethod # known case of __new__
    def __new__(S, *more): # real signature unknown; restored from __doc__
        """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
        pass

    def __ne__(self, y): # real signature unknown; restored from __doc__
        """ x.__ne__(y) <==> x!=y """
        pass

    def __repr__(self): # real signature unknown; restored from __doc__
        """ x.__repr__() <==> repr(x) """
        pass

    def __reversed__(self): # real signature unknown; restored from __doc__
        """ L.__reversed__() -- return a reverse iterator over the list """
        pass

    def __rmul__(self, n): # real signature unknown; restored from __doc__
        """ x.__rmul__(n) <==> n*x """
        pass

    def __setitem__(self, i, y): # real signature unknown; restored from __doc__
        """ x.__setitem__(i, y) <==> x[i]=y """
        pass

    def __setslice__(self, i, j, y): # real signature unknown; restored from __doc__
        """
        x.__setslice__(i, j, y) <==> x[i:j]=y

                   Use  of negative indices is not supported.
        """
        pass

    def __sizeof__(self): # real signature unknown; restored from __doc__
        """ L.__sizeof__() -- size of L in memory, in bytes """
        pass

    __hash__ = None

list

列表方法

元组:带上枷锁的列表,方法同列表,但元组不可更改

如果想更新元组:temp=(1,2,3,4)           temp=temp(:2)+(‘hello‘,)+temp(2:)

字典:{键:值,键:值。。。}

创建字典:

person = {"name": "mr.wu", ‘age‘: 18}
或
person = dict({"name": "mr.wu", ‘age‘: 18})

常用操作:

  • 索引
  • 新增
  • 删除  del dict[key]
  • 键、值、键值对
  • 循环
  • 长度
for i in 字典:
    print(i) #默认输出key
for k,v in 字典:
    print(k)
    print(v)

其他:

enumrate

为可迭代的对象添加序号

li = [11,22,33]
for k,v in enumerate(li, 1):   #默认从0开始自增,这里是从1开始自增
    print(k,v)  #打印 1,11    2,22    3,33

range和xrange

指定范围,生成指定的数字

2.7中range(1,10),直接就创建1-9,xrange(1,10),只有在迭代的时候才创建1-9,提高效率

print range(1, 10)
# 结果:[1, 2, 3, 4, 5, 6, 7, 8, 9]

print range(1, 10, 2)
# 结果:[1, 3, 5, 7, 9]

print range(30, 0, -2)
# 结果:[30, 28, 26, 24, 22, 20, 18, 16, 14, 12, 10, 8, 6, 4, 2]  

3.5中的range就相当于2.7中的xrange

  

时间: 2024-10-23 14:52:31

数据类型方法的相关文章

Python数据类型方法精心整理,不必死记硬背,看看源码一切都有了

Python认为一切皆为对象:比如我们初始化一个list时: li = list('abc') 实际上是实例化了内置模块builtins(python2中为__builtin__模块)中的list类: class list(object): def __init__(self, seq=()): # known special case of list.__init__ """ list() -> new empty list list(iterable) ->

Python数据类型方法简介一————字符串的用法详解

符串是Python中的重要的数据类型之一,并且字符串是不可修改的. 字符串就是引号(单.双和三引号)之间的字符集合.(字符串必须在引号之内,引号必须成对) 注:单.双和三引号在使用上并无太大的区别: 引号之间可以采取交叉使用的方式避免过多转义: 单.双引号实现换行必须使用续行符,而三引号可直接续行不必使用续行符号. a. count,统计字符或子字符串在字符串中出现的次数 格式:S.count(sub[, start[, end]]) -> int sub 是字符串中要查询的子字符串  star

datatable数据类型方法

Datatable数据类型介绍 简介: 这里介绍个在开发中经常用到的数据类型,数据类型为datatable.从数据库中查出的数据存放在datatable,但是很多情况下需要对查出的数据处理,这就需要积累些datatable方法,这样开发会更快. 方法介绍 用法一.声明一个datatable类型 通过声明自己创建一个datatable类型,并填充数据 DataTable dt = newDataTable(); dt.Columns.Add("Username"); dt.Columns

js基本数据类型+判断数据类型方法

摘要:不管是什么类型的,Object.prototype.toString.call();都可以判断出其具体的类型,简单基本类型(String.Number.Boolean.Null.Undefined)不是对象,复杂基本类型都为对象子类型,函数是特殊的对象子类型(可调用对象) 数据类型分为基本类型和引用类型: 基本类型:String.Number.Boolean.Null.Undefined.symbol(ES6) 引用类型:Object.Array.Date.Function.Error.R

JS判断数据类型方法

var a = "iamstring.";var b = 222;var c= [1,2,3];var d = new Date();var e = function(){alert(111);};var f = function(){this.name="22";}; **1.最常见的判断方法:typeof **alert(typeof a) ------------> stringalert(typeof b) ------------> numbe

Jquery / js 判断数据类型方法

当想要判断文本框中的值是否为自己想要的类型时,可以通过一些方法作出判断,这里对于光标离开文本框时判断文本框中输入的是否是数值类型,如果不是,做出提示 $("#WORKYEARS").blur(function () {//光标离开事件 var WORKYEARS = $.trim($("#WORKYEARS").val());//取出文本框的值 if (WORKYEARS != "") { var isok = isNumber(WORKYEAR

数据类型——方法总结(可能有不对的)

假设:var arr=['100px','abc'-6,[],-98765,34,-2,0,'300', ,function(){alert(1);},null,document,[],true,'200px'-30,'23.45元',5,Number('abc'),function(){alert(3);}]; 1.判断一个数是整数还是小数 if(parseInt(num)===parseFloat(num)) { console.log(num + '是整数'); }else{ consol

Python学习(四)—— 数据类型方法解析

一. int类型: 1. bit_length 原型:def bit_length(self): 功能:返回int型所占字节数 示例: a = 124 print(bin(a)) print(a.bit_length()) 结果: 0b1111100 7 二.str类型 1. capitalize 原型:def capitalize(self): 功能:字符串首字母大写 示例: s = "abc" print(s.capitalize()) 结果:"Abc" 2.c

python基础之数据类型方法使用

一. str 常用的方法主要有:(在pycharm界面中,在方法的位置按Ctrl + 左键可以查看方法的详细使用信息) len,count, join,strip,isdigit,replace,title,startswith,endswith,expandtabs,format,find,index 说明其中几个如下: #占位符的使用 a = "qiu {0} , xiao {1}" b = a.format('ni',33) print(b) #输出结果:----b中的ni,33