1, the string--is immutable
Definition and initialization
In [577]: s = ' Hello wolrd ' in [578]: sout[578]: ' Hello wolrd ' in [579]: s = "Hello python" in [580]: sout[580]: ' Hello Pytho n ' in [581]: s = ' ' Hello ' ' in [582]: sout[582]: ' Hello ' in [583]: s = "" "Hello" "[584]: sout[584]: ' Hello ' in [585]: in [ 585]: s = "' Hello python ...: I like Python '" in [586]: sout[586]: ' Hello python\ni like python '
Escape
In [587]: s = ' I like \npython ' in [588]: sout[588]: ' I like \npython ' in [589]: print (s) I like Pythonin [590]: s = ' i\ ' m ma Rtin ' 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 ' # plus R prefix means this string is a natural string and will not escape in [598]: Print (path) c:\\windows \nt\systemew
Subscript operation
In [599]: sout[599]: "I-M Martin" in [+]: 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 ']
Manipulation of strings
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 (out[624]: ' test ' in [625]: s = ' Hahahah hehe ' in [626]: s.strip () out[626]: ' Hahahah hehe '
list, tuple, string, bytes, ByteArray
are sequentially stored, sequentially accessed, and are iterative objects that can be accessed through an index
In [629]: List (enumerate ([+)]) out[629]: [(0, 1), (1, 2), (2, 3)]in [630]: LST = list (range) 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:]ou T[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]
This article from "Thick tak" blog, declined reprint!
Python Learning notes-built-in data structure 2