python學習筆記--內建資料結構2

來源:互聯網
上載者:User

標籤:python基礎

1、字串--是不可變的

定義及初始化

In [577]: s = ‘hello wolrd‘In [578]: sOut[578]: ‘hello wolrd‘In [579]: s = "hello python"In [580]: sOut[580]: ‘hello python‘In [581]: s = ‘‘‘hello‘‘‘In [582]: sOut[582]: ‘hello‘In [583]: s = """hello"""In [584]: sOut[584]: ‘hello‘In [585]: In [585]: s = ‘‘‘hello python     ...: i like python‘‘‘In [586]: sOut[586]: ‘hello python\ni like python‘


轉義

In [587]: s = ‘i like \npython‘In [588]: sOut[588]: ‘i like \npython‘In [589]: print(s)i like pythonIn [590]: s = ‘i\‘m martin‘In [591]: sOut[591]: "i‘m martin"In [592]: path = ‘C:\\windows\nt\systemew‘In [593]: pathOut[593]: ‘C:\\windows\nt\\systemew‘In [594]: print(path)C:\windowst\systemewIn [595]: path = ‘C:\\windows\\nt\systemew‘In [596]: print(path)C:\windows\nt\systemewIn [597]: path =r‘C:\\windows\nt\systemew‘  # 加r首碼代表此字串是自然字串, 不會轉義In [598]: print(path)C:\\windows\nt\systemew


下標操作

In [599]: sOut[599]: "i‘m martin"In [600]: s[3]Out[600]: ‘ ‘In [601]: s[0]Out[601]: ‘i‘In [602]: type(s[0])Out[602]: strIn [603]: In [603]: s = ‘martin‘In [604]: for i in s:   #字串是可迭代對象     ...:     print(i)     ...:     martinIn [605]: list(s)Out[605]: [‘m‘, ‘a‘, ‘r‘, ‘t‘, ‘i‘, ‘n‘]


字串的操作

In [607]: lst = [‘i‘,‘am‘,‘martin‘]In [608]: lstOut[608]: [‘i‘, ‘am‘, ‘martin‘]In [609]: ‘ ‘.join(lst)Out[609]: ‘i am martin‘In [610]: In [610]: s = ‘i love python‘In [611]: s.split()Out[611]: [‘i‘, ‘love‘, ‘python‘]In [612]: ‘      love python‘.split()Out[612]: [‘love‘, ‘python‘]In [613]: ‘      love python‘.split(‘ ‘)Out[613]: [‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘love‘, ‘python‘]In [614]: s.split(maxsplit=1)Out[614]: [‘i‘, ‘love python‘]In [615]: s = ‘‘‘i am martin     ...: i love python‘‘‘In [616]: s.splitlines()Out[616]: [‘i am martin‘, ‘i love python‘]In [617]: ‘i am martin‘.partition(‘ ‘)Out[617]: (‘i‘, ‘ ‘, ‘am martin‘)In [618]: cfg = ‘env = PATH=/usr/bin;$PATH‘In [619]: cfg.partition(‘=‘)Out[619]: (‘env ‘, ‘=‘, ‘ PATH=/usr/bin;$PATH‘)In [620]: s = ‘test‘In [621]: s.upper()Out[621]: ‘TEST‘In [622]: s.upper().lower()Out[622]: ‘test‘In [623]: s.title()Out[623]: ‘Test‘In [624]: s.center(30)Out[624]: ‘             test             ‘In [625]: s = ‘hahahah   hehe‘In [626]: s.strip()Out[626]: ‘hahahah   hehe‘

列表, 元組, 字串, bytes, bytearray

都是順序儲存, 順序訪問的, 都是可迭代對象, 都可以通過索引訪問

In [629]: list(enumerate([1,2,3]))Out[629]: [(0, 1), (1, 2), (2, 3)]In [630]: lst = list(range(10))In [631]: lstOut[631]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]In [632]: lst[3]Out[632]: 3In [633]: lst[3:7]Out[633]: [3, 4, 5, 6]In [634]: lst[3:7:2]Out[634]: [3, 5]In [635]: lst[3:]Out[635]: [3, 4, 5, 6, 7, 8, 9]In [636]: lst[:7]Out[636]: [0, 1, 2, 3, 4, 5, 6]In [637]: lst[:]Out[637]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]In [638]: lst[8:5:-1]Out[638]: [8, 7, 6]


本文出自 “厚德載物” 部落格,謝絕轉載!

python學習筆記--內建資料結構2

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.