学习总结---列表和字符串的比较

前言

该文章描述了列表以及字符串的比较以及理解对象的方法

2020-01-16

天象独行

  在对列表和字符串的比较之前,我们先尝试初步理解一下“对象”这个概念,我们都知道“万物皆对象”。那么我们可以认为列表以及字符串都是对象。对象是有三个特性,即身份,类型,值。“身份”我们可以认为是物理地址,“类型”以字符串为例是str类型,列表为例list类型。“值”以字符串来说就是本身,列表来说就是其中的元素。那么,对于对象来说它含有一些对应的方法。一般我们采用dir()函数来查看对象的方法,help()函数来查看该方法如何使用。下面举例:

  查看字符串的方法:

a = 'qwerftgyhjuki'
print(dir(a))
C:UsersaaronDesktopPytoon-cadevenvScriptspython.exe C:/Users/aaron/Desktop/Pytoon-cade/urllib-Study.py
['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

Process finished with exit code 0

  查看字符串方法的使用

a = 'qwerftgyhjuki'
help(a.count)
C:UsersaaronDesktopPytoon-cadevenvScriptspython.exe C:/Users/aaron/Desktop/Pytoon-cade/urllib-Study.py
Help on built-in function count:

count(...) method of builtins.str instance
    S.count(sub[, start[, end]]) -> int
    
    Return the number of non-overlapping occurrences of substring sub in
    string S[start:end].  Optional arguments start and end are
    interpreted as in slice notation.


Process finished with exit code 0

  下面我们来讨论字符串以及列表的区别相同点:

  0X01;相同点

    字符串和列表都是属于序列类型,即关于序列类型的基本操作字符串以及列表都是可以。比如:索引,分片,连接,重复等操作。

  0X02;不同点

    1;列表和字符串的最大区别是:列表是可以改变,字符串不可变。

    2;在字符串中,每个元素只能是字符,在列表中,元素可以是任何类型。

    3;字符串和列表可以相互转化(利用函数:split()以及join())

 

原文地址:https://www.cnblogs.com/aaron456-rgv/p/12202383.html