Python內置數據結構2

字符串及其操做

  • string 的定義(''' 三個引號)
  • \ 轉義符號
  • r'' 表明字符串是天然字符串
  • string的下標操做
  • join
  • split, partition
  • upper(), lower()
  • replace, strip
  • find, index, count

1、字符串定義:

In [1]: s = 'miracle young'

In [2]: s = "miracle young"

In [3]: s = '''miracle young'''

三個引號(單雙)能夠定義多行字符串python

In [4]: s = '''
   ...: miracle young
   ...: is handsome
   ...: '''

In [5]: s = '
  File "<ipython-input-5-5a20d2d986f5>", line 1
    s = '
         ^
SyntaxError: EOL while scanning string literal


In [6]:

2、字符串相關操做

一、轉義  (\) or (r'')

In [13]: s = 'miracle young's wife very happy'
  File "<ipython-input-13-312ae38bd009>", line 1
    s = 'miracle young's wife very happy'
                       ^
SyntaxError: invalid syntax


In [14]: s = 'miracle young\'s wife very happy'

In [15]: s
Out[15]: "miracle young's wife very happy"

In [16]: print(s)
miracle young's wife very happy

In [17]:
In [7]: path = 'C:\windows\nt\system32'

In [8]: print(path)
C:\windows
t\system32

In [9]: path = r'C:\windows\nt\system32'

In [10]: print(path)
C:\windows\nt\system32

In [11]:

    r''表示是一段天然字符串mysql

二、下標操做

In [11]: s = 'miracle young papapapa'

In [12]: for ss in s:
    ...:     print(ss)
    ...:
m
i
r
a
c
l
e

y
o
u
n
g

p
a
p
a
p
a
p
a

In [13]: s[0]
Out[13]: 'm'

In [14]: s[2]
Out[14]: 'r'

In [15]: s[-1]
Out[15]: 'a'

In [16]: s[-100]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-16-3eaddaa60d3d> in <module>()
----> 1 s[-100]

IndexError: string index out of range

In [17]: s[100]
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-17-2a138df92e52> in <module>()
----> 1 s[100]

IndexError: string index out of range

In [18]:
In [18]: c = list(s)

In [19]: c
Out[19]:
['m',
 'i',
 'r',
 'a',
 'c',
 'l',
 'e',
 ' ',
 'y',
 'o',
 'u',
 'n',
 'g',
 ' ',
 'p',
 'a',
 'p',
 'a',
 'p',
 'a',
 'p',
 'a']

In [20]: len(s)
Out[20]: 22

In [21]: s.count('m')
Out[21]: 1

In [22]: s.index('m')
Out[22]: 0

In [23]: s + ' wife'
Out[23]: 'miracle young papapapa wife'

In [24]:

三、join

In [27]: l = ['miracle', 'young', 'papapa']

In [28]: new_s = ' '.join(l)  #返回結果,原地不作修改

In [29]: new_s
Out[29]: 'miracle young papapa'

In [30]: new_s = ','.join(l)

In [31]: new_s
Out[31]: 'miracle,young,papapa'

In [32]:

四、分隔

    1)  s.split()

In [43]: help(s.split)

Help on built-in function split:

split(...) method of builtins.str instance
    S.split(sep=None, maxsplit=-1) -> list of strings

    Return a list of the words in S, using sep as the
    delimiter string.  If maxsplit is given, at most maxsplit
    splits are done. If sep is not specified or is None, any
    whitespace string is a separator and empty strings are
    removed from the result.
~

(END)
In [48]: s = '  papa pa pa   '

In [49]: s.split()  # 返回結果, 原地不作修改
Out[49]: ['papa', 'pa', 'pa']

In [50]: s
Out[50]: '  papa pa pa   '

In [51]: s.split('  ', 1)
Out[51]: ['', 'papa pa pa   ']

In [52]: s.split('  ')
Out[52]: ['', 'papa pa pa', ' ']

In [53]: s.split('pa')  # split 的分隔符能夠是任意多個字符串
Out[53]: ['  ', '', ' ', ' ', '   ']

In [54]:
def _split(s, sep,  maxsplit):
    ret = []
    tmp = []
    i = 0
    for c in s:
        if c!= sep:
            tmp.append(c)
        else:
            i += 1
            ret.append(''.join(tmp))
            tmp.clear()
        if i >= maxsplit and maxsplit > 0:
            return ret
    return ret

    2) rsplit()

In [48]: s = '  papa pa pa   '

In [49]: s.split()  # 和s.split() 結果同樣 從右開始分隔
Out[49]: ['papa', 'pa', 'pa']

In [50]: s
Out[50]: '  papa pa pa   '

In [51]: s.split('  ', 1)
Out[51]: ['', 'papa pa pa   ']

In [52]: s.split('  ')
Out[52]: ['', 'papa pa pa', ' ']

In [53]: s.split('pa')
Out[53]: ['  ', '', ' ', ' ', '   ']

In [54]: s.rsplit()
Out[54]: ['papa', 'pa', 'pa']

In [55]: s.rsplit(' ', 1)
Out[55]: ['  papa pa pa  ', '']

In [56]:
def _rsplit(s, sep,  maxsplit):
    ret = []
    tmp = []
    i = 0
    for c in reversed(s):
        if c!= sep:
            tmp.append(c)
        else:
            i += 1
            ret.append(''.join(reversed(tmp)))
            tmp.clear()
        if i >= maxsplit and maxsplit > 0:
            return reversed(ret)
    return reversed(ret)

    3) spitlines()

In [1]: sql = '''
   ...: sfsdf
   ...: sdfsdfsdfsdf
   ...:
   ...: sdfsdfsdfsf
   ...:
   ...: sdfsfsdfsdfsfsfsd
   ...: '''

In [2]: sql.splitlines()
Out[2]: ['', 'sfsdf', 'sdfsdfsdfsdf', '', 'sdfsdfsdfsf', '', 'sdfsfsdfsdfsfsfsd']

In [3]: sql = '''
   ...: sfsdf
   ...: sdfsdfsdfsdf
   ...: sdfsdfsdfsf
   ...: sdfsfsdfsdfsfsfsd
   ...: '''

In [4]: sql.splitlines()
Out[4]: ['', 'sfsdf', 'sdfsdfsdfsdf', 'sdfsdfsdfsf', 'sdfsfsdfsdfsfsfsd']

In [5]:

    4)partition

In [6]: s = 'host = 127.0.0.1'

In [7]: help(s.partition)

Help on built-in function partition:

partition(...) method of builtins.str instance
    S.partition(sep) -> (head, sep, tail)

    Search for the separator sep in S, and return the part before it,
    the separator itself, and the part after it.  If the separator is not
    found, return S and two empty strings.
~
(END)
In [8]: s.split('=')
Out[8]: ['host ', ' 127.0.0.1']

In [9]: s.partition('=')
Out[9]: ('host ', '=', ' 127.0.0.1')

In [10]: cfg = 'mysql.connect = mysql://user:password@127.0.0.1:3306/test'

In [11]: cfg.partition('=')
Out[11]: ('mysql.connect ', '=', ' mysql://user:password@127.0.0.1:3306/test')

In [12]: cfg = 'env=PATH=/sur/bin:$PATH'

In [13]: cfg.partition('=')
Out[13]: ('env', '=', 'PATH=/sur/bin:$PATH')

In [14]:

    5)rpartition

In [14]: s = 'a=b=c'

In [15]: s.partition('=')
Out[15]: ('a', '=', 'b=c')

In [16]: s.rpartition('=')
Out[16]: ('a=b', '=', 'c')

In [17]:
In [17]: help(s.rpartition)

Help on built-in function rpartition:

rpartition(...) method of builtins.str instance
    S.rpartition(sep) -> (head, sep, tail)

    Search for the separator sep in S, starting at the end of S, and return
    the part before it, the separator itself, and the part after it.  If the
    separator is not found, return two empty strings and S.
~

(END)
def _partion(s, sep):
    if s == '':
        return '', '', ''
    tmp = s.split(sep, maxsplit=1)
    if (len(tmp)) == 2:
        return tmp[0], sep, tmp[1]
    elif len(tmp) == 1:
        return tmp[0], sep, ''
    elif len(tmp) == 0:
        return '', sep, ''

五、大小寫轉換

In [18]: s = 'miracle'

In [19]: s.upper()
Out[19]: 'MIRACLE'

In [20]: s.lower()
Out[20]: 'miracle'



In [22]: s.casefold()
Out[22]: 'miracle'

In [23]: s.swapcase()  # 大小寫互換
Out[23]: 'MIRACLE'

In [24]:

六、排版

In [25]: s
Out[25]: 'miracle'

In [26]: s.title()
Out[26]: 'Miracle'

In [27]: s = 'miracle young like dota and basketball.'

In [28]: s.title()
Out[28]: 'Miracle Young Like Dota And Basketball.'

In [29]: s.capitalize()
Out[29]: 'Miracle young like dota and basketball.'

In [30]: s = 'miracle young like dota and basketball and Girl.'

In [31]: s.capitalize()
Out[31]: 'Miracle young like dota and basketball and girl.'
In [32]: s.center(100)
Out[32]: '                          miracle young like dota and basketball and Girl.                          '

In [33]: help(s.center)
Help on built-in function center:

center(...) method of builtins.str instance
    S.center(width[, fillchar]) -> str

    Return S centered in a string of length width. Padding is
    done using the specified fill character (default is a space)
~
(END)
In [34]: s.center(100, '#')
Out[34]: '##########################miracle young like dota and basketball and Girl.##########################'

In [35]:
In [35]: help(s.zfill)
Help on built-in function zfill:

zfill(...) method of builtins.str instance
    S.zfill(width) -> str

    Pad a numeric string S with zeros on the left, to fill a field
    of the specified width. The string S is never truncated.
~
(END)
In [36]: s
Out[36]: 'miracle young like dota and basketball and Girl.'

In [37]: s.zfill(100)
Out[37]: '0000000000000000000000000000000000000000000000000000miracle young like dota and basketball and Girl.'

In [38]: str(1234).zfill(11)
Out[38]: '00000001234'


In [40]: '\t'.expandtabs()
Out[40]: '        '

In [41]:

七、修改

    1)s.replace()

In [41]: help(s.replace)
Help on built-in function replace:

replace(...) method of builtins.str instance
    S.replace(old, new[, count]) -> str

    Return a copy of S with all occurrences of substring
    old replaced by new.  If the optional argument count is
    given, only the first count occurrences are replaced.
~
(END)
In [42]: s = 'abcdefgdscesabcbc'

In [43]: s.replace('e', 'x')    # 返回結果集,原地不作修改。
Out[43]: 'abcdxfgdscxsabcbc'

In [44]: s
Out[44]: 'abcdefgdscesabcbc'

In [45]: s.replace('e', 'X', 1)
Out[45]: 'abcdXfgdscesabcbc'

In [46]: s.replace('e', 'X', 2)
Out[46]: 'abcdXfgdscXsabcbc'

In [47]: s.replace('abc', 'XYZ')
Out[47]: 'XYZdefgdscesXYZbc'

In [48]:

    2)  s.strip()

In [48]: help(s.strip)
Help on built-in function strip:

strip(...) method of builtins.str instance
    S.strip([chars]) -> str

    Return a copy of the string S with leading and trailing
    whitespace removed.
    If chars is given and not None, remove characters in chars instead.
~
(END)
In [49]: s = '  pa pa  pa '

In [50]: s.strip()
Out[50]: 'pa pa  pa'

In [51]: s.strip(' ')
Out[51]: 'pa pa  pa'

In [52]: s.strip('  ')
Out[52]: 'pa pa  pa'

In [53]: s = 'ababbcased'

In [54]: s.strip('abd')
Out[54]: 'case'

    3) s.lstrip() and s.rstrip()

​In [55]: help(s.lstrip)
Help on built-in function lstrip:

lstrip(...) method of builtins.str instance
    S.lstrip([chars]) -> str

    Return a copy of the string S with leading whitespace removed.
    If chars is given and not None, remove characters in chars instead.
~
(END)
In [59]: help(s.rstrip)
Help on built-in function rstrip:

rstrip(...) method of builtins.str instance
    S.rstrip([chars]) -> str

    Return a copy of the string S with trailing whitespace removed.
    If chars is given and not None, remove characters in chars instead.
~
(END)
In [56]: s
Out[56]: 'ababbcased'

In [57]: s.lstrip('ad')
Out[57]: 'babbcased'

In [58]: s.rstrip('ad')
Out[58]: 'ababbcase'

In [59]:

    4) s.ljust() and s.rjust()

In [64]: help(s.ljust)
Help on built-in function ljust:

ljust(...) method of builtins.str instance
    S.ljust(width[, fillchar]) -> str

    Return S left-justified in a Unicode string of length width. Padding is
    done using the specified fill character (default is a space).
~
(END)
In [65]: help(s.rjust)
Help on built-in function rjust:

rjust(...) method of builtins.str instance
    S.rjust(width[, fillchar]) -> str

    Return S right-justified in a string of length width. Padding is
    done using the specified fill character (default is a space).
~
(END)
In [60]: s
Out[60]: 'ababbcased'

In [61]: s.ljust(10)
Out[61]: 'ababbcased'

In [62]: s.ljust(100)
Out[62]: 'ababbcased                                                                                                                                                                              '

In [63]: s.rjust(100)
Out[63]: '                                                                                                                                                                              ababbcased'

In [64]:
In [66]: s.ljust(100, '#')
Out[66]: 'ababbcased##########################################################################################'

In [67]: s.rjust(100, '#')
Out[67]: '##########################################################################################ababbcased'

In [68]: s.rjust(100, '##')
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-68-b458ded36f17> in <module>()
----> 1 s.rjust(100, '##')

TypeError: The fill character must be exactly one character long

In [69]:

八、查找

    1) s.find()

In [69]: s = 'i love miracle young love nice girl'

In [70]: help(s.find)
Help on built-in function find:

find(...) method of builtins.str instance
    S.find(sub[, start[, end]]) -> int

    Return the lowest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.
~
(END)
In [71]: s.find('love')   # 返回第一個匹配的字符串,返回的值是字串的第一個字符索引
Out[71]: 2

In [72]: s.find('love', 3)
Out[72]: 21

In [73]: s.find('love', 3, 5)  # 在索引 start 3  end 5 範圍內查找 'love'沒有找到則返回 -1
Out[73]: -1

In [74]:

    2) s.rfind()

In [74]: help(s.rfind)
Help on built-in function rfind:

rfind(...) method of builtins.str instance
    S.rfind(sub[, start[, end]]) -> int

    Return the highest index in S where substring sub is found,
    such that sub is contained within S[start:end].  Optional
    arguments start and end are interpreted as in slice notation.

    Return -1 on failure.
~
(END)
In [75]: s
Out[75]: 'i love miracle young love nice girl'

In [76]: s.rfind('love')    # 從右往左查找, 返回的仍是正常的整數索引
Out[76]: 21

In [77]: s.index('love')
Out[77]: 2

In [78]: s.index('love', 3)
Out[78]: 21

In [79]: s.index('love', 3, 5)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-79-b1b620f7f23e> in <module>()
----> 1 s.index('love', 3, 5)

ValueError: substring not found


In [80]: s.rindex('love')
Out[80]: 21

In [81]: s.rindex('love', 3)
Out[81]: 21

In [82]: s.rindex('love', 3, 5)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-82-79f500d583eb> in <module>()
----> 1 s.rindex('love', 3, 5)

ValueError: substring not found

In [83]:
  • find 查找,字串不存在時,返回 -1
  • index 查找,字串不存在時,返回ValueError

    3) s.count()

In [84]: s
Out[84]: 'i love miracle young love nice girl'

In [85]: help(s.count)
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.
~
(END)
In [86]: s.count('very')
Out[86]: 0

In [87]: s.count('a')
Out[87]: 1

In [88]: s.count('o')
Out[88]: 3

    4) s.startswith() and s.endswith()

In [92]: help(s.startswith)
Help on built-in function startswith:

startswith(...) method of builtins.str instance
    S.startswith(prefix[, start[, end]]) -> bool

    Return True if S starts with the specified prefix, False otherwise.
    With optional start, test S beginning at that position.
    With optional end, stop comparing S at that position.
    prefix can also be a tuple of strings to try.
~
(END)
In [93]: help(s.endswith)
Help on built-in function endswith:

endswith(...) method of builtins.str instance
    S.endswith(suffix[, start[, end]]) -> bool

    Return True if S ends with the specified suffix, False otherwise.
    With optional start, test S beginning at that position.
    With optional end, stop comparing S at that position.
    suffix can also be a tuple of strings to try.
~
(END)
In [94]: s
Out[94]: 'i love miracle young love nice girl'

In [95]: s.startswith('i love')
Out[95]: True

In [96]: s.endswith('girl')
Out[96]: True

In [97]: s.endswith('girl.')
Out[97]: False

In [98]:

    5) s.isalnum()  s.istitle()  s.isdentifier()

In [98]: help(s.isalnum)
Help on built-in function isalnum:

isalnum(...) method of builtins.str instance
    S.isalnum() -> bool

    Return True if all characters in S are alphanumeric
    and there is at least one character in S, False otherwise.
~
(END)
In [99]: s.isalnum()
Out[99]: False

In [100]: '1234'.isalnum()
Out[100]: True

In [101]: 'a1234'.isalnum()
Out[101]: True

In [102]: 'a1a'.isalnum()
Out[102]: True

 

In [103]: help(s.istitle)
Help on built-in function istitle:

istitle(...) method of builtins.str instance
    S.istitle() -> bool

    Return True if S is a titlecased string and there is at least one
    character in S, i.e. upper- and titlecase characters may only
    follow uncased characters and lowercase characters only cased ones.
    Return False otherwise.
~
(END)
In [107]: s.istitle()
Out[107]: False

In [108]: s.title().istitle()
Out[108]: True

In [109]:

 

In [113]: help(s.isidentifier)
Help on built-in function isidentifier:

isidentifier(...) method of builtins.str instance
    S.isidentifier() -> bool

    Return True if S is a valid identifier according
    to the language definition.

    Use keyword.iskeyword() to test for reserved identifiers
    such as "def" and "class".
~
(END)
In [114]: s.isidentifier()
Out[114]: False

In [115]: 'a_b'.isidentifier()
Out[115]: True

In [116]:
相關文章
相關標籤/搜索