內建資料結構-字串

weixin_34321977發表於2017-01-08

字串是一種序列,支援切片操作,但是不可變,在python3中是unicode序列,在2中是byte序列

字串連線

+號可以連線多個字串,但是不推薦,因為每次連線都會生成新的物件

join連線的列表中的元素必須全部是字串
In [136]: lst3
Out[136]: ['a', 3, 2, 1, 'hello list', 'a', 10, 'head']

In [137]: ''.join(lst3)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-137-81ba8f3237dc> in <module>()
----> 1 ''.join(lst3)

TypeError: sequence item 1: expected str instance, int found

In [138]: l = ['life', 'is', 'short', 'I', 'use', 'python']



In [139]: ''.join(l)
Out[139]: 'lifeisshortIusepython'

In [140]: ' '.join(l)
Out[140]: 'life is short I use python'

字串分割

split()方法,預設以空白字元分割, 可以指定分隔符和分隔的次數,從左向右分隔

In [142]: s
Out[142]: 'life is short I use python'

In [143]: s.split()
Out[143]: ['life', 'is', 'short', 'I', 'use', 'python']

In [144]: s2 = 'a\tb\tc'

In [146]: s2
Out[146]: 'a\tb\tc'

In [147]: s2.split()
Out[147]: ['a', 'b', 'c']

In [148]: s2.split('\t')
Out[148]: ['a', 'b', 'c']

In [149]: s2.split('\t', 1)
Out[149]: ['a', 'b\tc']

rsplit(),與split類似,但是順便是從右向左分隔
splitlines()按行分隔,引數True表示分隔後保留換行符

In [150]: s3 = 'hello,world\nhello python\nhello you can do it'

In [151]: s3.splitlines()
Out[151]: ['hello,world', 'hello python', 'hello you can do it']

In [152]: s3.splitlines(True)
Out[152]: ['hello,world\n', 'hello python\n', 'hello you can do it']

partition(), rpartition()返回一個三個元素的元組

In [153]: s3.partition('\n')
Out[153]: ('hello,world', '\n', 'hello python\nhello you can do it')


In [154]: s3.rpartition('\n')
Out[154]: ('hello,world\nhello python', '\n', 'hello you can do it')

字串的修改

capitalize() 將第一個單詞首字母小寫轉成大寫,其他的大寫換成小寫

In [155]: s
Out[155]: 'life is short I use python'

In [156]: s.capitalize()
Out[156]: 'Life is short i use python'

title()將每個單詞的首字母變成大寫

In [163]: s
Out[163]: 'life is short I use python'

In [164]: s.title()
Out[164]: 'Life Is Short I Use Python'

lower() ,upper()將所有的字母轉成小寫/大寫

In [165]: s
Out[165]: 'life is short I use python'

In [166]: s.lower()
Out[166]: 'life is short i use python'

In [167]: s.upper()
Out[167]: 'LIFE IS SHORT I USE PYTHON'

swapcase()大小寫互相轉換

In [168]: s.swapcase()
Out[168]: 'LIFE IS SHORT i USE PYTHON'

center()字串居中

In [169]: s.center(50)
Out[169]: '            life is short I use python            '

In [170]: s.center(50, '#')
Out[170]: '############life is short I use python############'

rjust(), ljust()讓字串在右邊、左邊顯示

In [171]: s.ljust(50)
Out[171]: 'life is short I use python                        '

In [172]: s.ljust(50, '$')
Out[172]: 'life is short I use python$$$$$$$$$$$$$$$$$$$$$$$$'

zfill() 如果給定的長度大於字串的長度,用0填充

In [175]: s.zfill(50)
Out[175]: '000000000000000000000000life is short I use python'

readline()逐行讀取
readlines()一次全部讀取

startswith() 以什麼字元開頭,可以帶下標
endswith() 以什麼字元結尾

In [1]: s = '## Life is short, I use Python!!!'

In [2]: s.startswith('#')
Out[2]: True

In [3]: s.startswith('#',3)
Out[3]: False

In [4]: s.startswith('#',2,5)
Out[4]: False

In [5]: s.startswith('#',1,5)
Out[5]: True

In [6]: s.endswith('!')
Out[6]: True

In [7]: s.endswith('!',2,9)
Out[7]: False

字串查詢替換

  • count 統計單個或者多個字元的個數
  • find 從左向右查詢字串的位置,可以有下標引數,沒找到返回-1
  • rfind 從右向左查詢,可以有下標引數,沒找到返回-1
  • indexfind類似,但是找不到會丟擲異常
  • rindex 從右向左查詢
  • replace 從左向右替換,可以接替換的次數
In [8]: s
Out[8]: '## Life is short, I use Python!!!'

In [9]: s.count('i')
Out[9]: 2

In [10]: s.count('##')
Out[10]: 1

In [11]: s.find('I')
Out[11]: 18

In [12]: s.find('Life')
Out[12]: 3

In [13]: s.rfind('Life')
Out[13]: 3

In [14]: s.rfind('i')
Out[14]: 8

In [15]: s.find('i')
Out[15]: 4

In [16]: s.find('i',10)
Out[16]: -1

In [17]: s.find('ii',10)
Out[17]: -1

In [18]: s.index('i',10)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-18-bba1a8a7f910> in <module>()
----> 1 s.index('i',10)

ValueError: substring not found

In [25]: s
Out[25]: '## Life is short, I use Python!!!'

In [26]: s.replace('use', 'love')
Out[26]: '## Life is short, I love Python!!!'

字串的格式化

  • printf 風格 類似C語言中的printf ,在2中使用的較多,3中很少使用
    前面是一個字串模板,後面接一個元組或者字典
In [27]: 'I love %s' % ('Python',)
Out[27]: 'I love Python'

In [28]: '%s is open source and %s is simple' %('python', 'python')
Out[28]: 'python is open source and python is simple'

當一個模板中有多處使用同一個佔位符的時候使用字典就比較簡潔
In [29]: '%(name)s is open source and %(name)s is simple' %{'name': 'python'}
Out[29]: 'python is open source and python is simple'
  • format 方法 也是模板 呼叫format()方法
In [30]: '{0} is open source and {0} is simple'.format('python')
Out[30]: 'python is open source and python is simple'

In [31]: '{} is open source and {} is simple'.format('python')
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-31-d48de5a44c97> in <module>()
----> 1 '{} is open source and {} is simple'.format('python')

IndexError: tuple index out of range

In [32]: '{} is open source and {} is simple'.format('python', 'python')
Out[32]: 'python is open source and python is simple'

In [33]: '{0} is open source and {1} is simple'.format('python', 'python')
Out[33]: 'python is open source and python is simple'

In [34]: '{name} is open source and {name} is simple'.format(name='python')
Out[34]: 'python is open source and python is simple'

In [35]: 'my name is {name}, I am {age}'.format(name='loveroot', age=18)
Out[35]: 'my name is loveroot, I am 18'

關鍵字引數要放在後面 否則會有引數位置錯誤
In [37]: '{1} {0} {name}'.format('world', 'Hello', name='!')
Out[37]: 'Hello world !'

In [38]: '{1} {0} {name} {2}'.format('world', 'Hello', name='!', '!!!')
  File "<ipython-input-38-f15963a1a293>", line 1
    '{1} {0} {name} {2}'.format('world', 'Hello', name='!', '!!!')
                                                           ^
SyntaxError: positional argument follows keyword argument

傳入物件與下標操作

In [39]: class A:
    ...:     def __init__(self):
    ...:         self.x = 1
    ...:         self.y = 2
    ...:         

In [40]: a = A()

In [41]: '{0.x}, {0.y}'.format(a)
Out[41]: '1, 2'

In [42]: l = ['hello', 'world']

In [43]: '{0[0]}, {0[1]}'.format(l)
Out[43]: 'hello, world'

填充與對齊

  • 填充常跟對齊一起使用^<>+-分別是居中左對齊右對齊,正數, 負數,後面頻寬度:號後面帶填充的字元,只能是一個字元,不指定的話預設是用空格填充
    比如
右對齊 寬度為8
In [15]: '{:>8}'.format('189')
Out[15]: '   189'

用0填充空白
In [16]: '{:0>8}'.format('189')
Out[16]: '00000189'

In [17]: '{:a>8}'.format('189')
Out[17]: 'aaaaa189'

In [57]: '{0:-}'.format(1100)
Out[57]: '1100'

In [58]: '{0:-}'.format(-1100)
Out[58]: '-1100'

In [59]: '{0:+}'.format(-1100)
Out[59]: '-1100'

精度與型別f

其中.2表示長度為2的精度,f表示float型別。
In [44]: '{:.2f}'.format(321.33345)
Out[44]: '321.33'

其他型別
In [54]: '{:b}'.format(17)
Out[54]: '10001'
In [55]: '{:d}'.format(17)
Out[55]: '17'
In [56]: '{:o}'.format(17)
Out[56]: '21'
In [57]: '{:x}'.format(17)
Out[57]: '11'

用,號還能用來做金額的千位分隔符。

In [47]: '{:,}'.format(1234567890)
Out[47]: '1,234,567,890'

相關文章