學習總結---列表和字符串的比較


前言

該文章描述了列表以及字符串的比較以及理解對象的方法

2020-01-16

天象獨行

  在對列表和字符串的比較之前,我們先嘗試初步理解一下“對象”這個概念,我們都知道“萬物皆對象”。那么我們可以認為列表以及字符串都是對象。對象是有三個特性,即身份,類型,值。“身份”我們可以認為是物理地址,“類型”以字符串為例是str類型,列表為例list類型。“值”以字符串來說就是本身,列表來說就是其中的元素。那么,對於對象來說它含有一些對應的方法。一般我們采用dir()函數來查看對象的方法,help()函數來查看該方法如何使用。下面舉例:

  查看字符串的方法:

a = 'qwerftgyhjuki'
print(dir(a))
C:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.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:\Users\aaron\Desktop\Pytoon-cade\venv\Scripts\python.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())

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM