字串

處理字串能力(邏輯能力)

  • 沒寫過程式的人通常會認為程式設計員的數學都很厲害,因為他們經常與數學打交道。

  • 事實上大多數的程式員處理字串的次數遠比數字還要多。通常邏輯(與創造性)思考能力遠比數學技能重要。

Unicode標準

  • Python 3 支援 Unicode 標準,所以它可以包容任何語言文字及許多符號。可以處理這種標準能力,是它與 Python 2 分開的主因。本書會在各地方討論 Unicode,因它有時令人望而生畏。

  • 本書接下來的字串範例中,大多會使用 ASCII範例。

Python 序列(sequence)

  • 字串是我們第一個 Python 序列(sequence)範例。
  • 與其他語言不同的是,Python的字元是不可變的。不能直接改變字串,但可以將一部分的字串複製到其他字串,來產生相同效果。

使用引號

  • 引號有「單引號''」、「雙引號""」、「三個單引號'''」、「三個雙引號"""」

使用單引號或雙引號

製作 Python 字串,使用單引號或雙引號來框住字串

>>> 'snap'
'snap'

>>> "Crackle"
'Crackle'

單引號 + 雙引號

  • 有兩種引號字元,主要的目的是為了可以建立內含引號字元的字串。可以在雙引號字串中加入單引號,或單引號字串中加入雙引號。
>>> "'Nay,' said the naysayer."
"'Nay,' said the naysayer."

>>> 'The rare double quote in captivity: ".'
'The rare double quote in captivity: ".'

>>> 'A "two by four" is actually 1 1/2" x 3 1/2".'
'A "two by four is" actually 1 1/2" x 3 1/2"

>>> "'There's the man that shot my paw!' cried the limping hound."
"'There's the man that shot my paw!' cried the limping hound."
  • 使用單引號來建立多行字串,python會在第二行出現例外
>>> poem = 'There was a young lady of Norway,

SyntaxError: EOL while scanning string literal

三個單引號(''')或三個雙引號(""")

短字串

就短字串而言,三引號並不是很實用。

>>> '''Boom!'''
'Boom'
>>>

多行字串

  • 三引號,它們最常見的用途是建立多行字串
  • 將多行文字包在三引號裡,字串會保留行尾字元。如果開頭或結果有空格,它們也會被保留。
>>> poem = '''There was a Young Lady of Norway,
    Who casually sat in a doorway;
    When the door squeezed her flat;
    She exclaimed, "What of that?"
    This courageous Young Lady of Norway.'''

空字串(用任何一種引號)

  • 空字串,完全沒有字元。但可以用任何一種引號來建立空字串。
>>> ''
''
>>> ""
''
>>> ''''''
''
>>> """"""
''
  • 為何需要空字串?想要使用「其他的字串」來建立「一個字串」,因此需要從空白開始。
>>> bottles = 99
>>> base = ''
>>> base += 'current inventory: '
>>> base += str(bottles)
>>> base

'current inventory: 99'

print()的輸出 v.s. 互動式解譯器的自動回應

print()

  • print()會將字串引號去除,並引出他們的內容。
  • 它是為了輸出給人類看的。會在印出的所有東西之間加上空格,並且在結尾加上換行符號。
>>> poem2 = '''I do not like thee, Doctor Fell.
        The reason why, I cannot tell.
        But this I know, and know full well:
        I do not like thee, Doctor Fell.
    '''

>>> print(poem2)
I do not like thee, Doctor Fell.
        The reason why, I cannot tell.
        But this I know, and know full well:
        I do not like thee, Doctor Fell.
>>> print(99, 'bottles', 'would be enough.')
99 bottles would be enough.

解譯器的自動回應

  • 解譯器會印出含有單引號與轉義字元(例如\n)的字串
>>> poem2
'I do not like thee, Doctor Fell.\n        The reason why, I cannot tell.\n        But this I know, and know full well:\n        I do not like thee, Doctor Fell.\n    '

轉義【用\】

  • Python可轉義字串中某些字元的含義,來產生其他方式難表達的效果。
  • 可在字元前面加上反斜線(\),來賦予特殊的含義。

\n(開始新的一行)

  • 最常見的轉義序列是\n,它代表開始新的一行
  • 透過它,可以用一行字串來建立多行的字串。
>>> palindrome = 'A man,\nA plan,\nA canal:\nPanama.'
>>> print(palindrome)
A man,
A plan,
A canal:
Panama.

\t(tab)

  • 使用轉義序列\t(tab)來對齊文字
>>> print('\tabc')
    abc

>>> print('a\tba')
a    bc

>>> print('abc\t')
abc

\'或 \"(代表字面的單或雙引號)

在一個用 ' 或 " 字元框起來的字串中,需要使用 \' 或 \" 來代表字面的單或雙引號

>>> testimony = "\"I did nothing!\" he said. \"Not that either! Or the other thing.\"
>>> print(testimony)
"I did nothing!" he said. "Not that either! Or the other thing."

>>> fact = "The world's largest rubber duck was 54'2\" by 65'7\" by 105'"
>>> print(fact)
The world's largest rubber duck was 54'2" by 65'7" by 105'

\(代表字面的反斜線)

字面的反斜線,只要鍵入兩個反斜線就可以了

>>> speech = 'Today we honor our friend, the backslash: \\.'
>>> print(speech)
Today we honor our friend, the backslash: \.

結合【使用 + 】

  • 串接字串時,Python 不會加空格,所以必須自己加上空格
  • 但是,print()陳述式每個引數之間會自動加上空格,並在結尾加上換行符號

運算子(結合「常值字串」或「字串變數」)

>>> 'Release the kraken! ' + 'At once!'
'Release the kraken! A once!'

一個字串 + 一個字串 → 常值字串(不是字串變數

>>> "My word! ""A gentleman caller!"
'My word! A gentleman caller!'

print()陳述式

>>> a = 'Duck.'
>>> b = a
>>> c = 'Grey Duck!'

>>> a + b + c
'Duck.Duck.Grey Duck!'

>>> print(a, b, c)
Duck. Duck. Grey Duck!

複製【使用 * 】

使用 * 來複製字串

>>> start = 'Na ' * 4 + '\n'
>>> middle = 'Hey ' * 3 + '\n'
>>> end = 'Goodbye.'
>>> print(start + start + middle + end)

Na Na Na Na 
Na Na Na Na 
Hey Hey Hey 
Goodbye.

擷取

擷取字元【使用 [] 】

除了字串,其他序列類型(串列與Tuple)的位移值也是相同用法(在本書第三章談到)

取出字串的字元,使用位移值

  • 要取出字串中的某個字元,可以在字串名稱後面的方括號中指定它的位移值
  • 第一個位移值(最左邊的位置)是 0,接下來是 1,以此類推
  • 最後一個(最右邊位置)是 -1,那裡開始往左是 -2、-3等
>>> letters = 'abcdefghijklmnopqrstuvwxyz'
>>> letters[0]
'a'
>>> letters[1]
'b'
>>> letters[-1]
'z'
>>> letters[-2]
'y'
>>> letters[25]
'z'
>>> letters[5]
'f'

指定位移值等於/超過字串長度,產生例外

  • 指定的位移值是字串長度,或比字串長(位移值是從 0 到長度 -1
>>> letters[100]
Traceback (most recent call last):
  File "<pyshell#26>", line 1, in <module>
    letters[100]
IndexError: string index out of range

>>> letters[26]
Traceback (most recent call last):
  File "<pyshell#27>", line 1, in <module>
    letters[26]
IndexError: string index out of range

字串是不可變、無法插入字元、更改字元

因為字串是不可變的,無法直接插入字元,或更改某個位移值的字元。否則會產生例外。

>>> name = 'Henny'
>>> name[0] = 'P'
Traceback (most recent call last):
  File "<pyshell#34>", line 1, in <module>
    name[0] = 'P'
TypeError: 'str' object does not support item assignment

修改字串,必須使用字串函式組合

修改字串,必須改用一些字串函式的組合,例如:replace()slice

>>> name = 'Henny'
>>> name.replace('H', 'P')
'Penny'
>>> name
'Henny'
>>> 'P' + name[1:]
'Penny'

擷取字串

使用 [開始:結束:間隔]的 Slice

  • 使用 slice 從字串中擷取一段子字串(部份的字串)。
  • 定義 slice(它們有些是可以省略的):[開始(start)位移值:結束(end)位移值:間隔(step)大小]
  • slice 比使用單一位移值,更能容許不良的位移值。例如:字串開頭之前的 slice 位移值會被視為 0,結尾之後的被視為 -1

  • slice 會包含位移值startend之前的那個字元。

    1. [:] 擷取整個序列,從開始到結束
    2. [start :] 指定從 start 到結束
    3. [: end] 指定從開始到 end 位移值減 1
    4. [start : end] 指定從 start 位移值到 end 位移值減 1
    5. [start : end : step] 會擷取從 start 位移值到 end 位移值減 1跳過 step 字元
  • slice 位移值
    • 開始:從 0、1 等往右移動
    • 結尾:從 -1、-2 等往左移動
    • 沒有指定:start 會使用 0,end 會使用 -1
    • end/step 是 -1 間隔值,會從結尾的地方開始,到start結束,不跳過任何字元

[:] = 0: = 整個字串

>>> letters = 'abcdefghijklmnopqrstuvwxyz'
>>> letters[:]
'abcdefghijklmnopqrstuvwxyz'

[start:]

從位移值 start 到結束。

>>> letters[20:]
'uvwxyz'

>>> letters[10:]
'klmnopqrstuvwxyz'

>>> letters[-3:]
'xyz'

>>> letters[-50:]
'abcdefghijklmnopqrstuvwxyz'

[:end]

從開始到 end 位移值減 1

>>> letters[:70]
'abcdefghijklmnopqrstuvwxyz'

[start:end]

從 start 位移值到 end 位移值減 1

>>> letters[12:15]
'mno'

>>> letters[18:-3]
'stuvw'

>>> letters[-6:-2]
'uvwx'

>>> letters[-51:-50]
''

>>> letters[70:71]
''

[start:end:step]

從位移值 start 到 end,間隔 step 個字元

>>> letters[4:20:3]
'ehknqt'

[:end:step]

  • 從開始到位移值 end,間隔 step 個字元
  • 位移值 end 必須是實際位移值加一
>>> letters[:21:5]
'afkpu'

[start::step]

  • 從位移值 start 到結束,間隔 step 個字元
  • -1的間隔值,會從結尾的地方開始,到start結束,不跳過任何字元
>>> letters[19::4]
'tx'

>>> letters[-1::-1]
'zyxwvutsrqponmlkjihgfedcba'

>>> letters[-1::-2]
'zxvtrpnljhfdb'

[::step] 只有間隔

  • 從開始到結束,間隔 step 字元(間隔 1 之外的大小)
  • -1的間隔值,會從結尾的地方開始,到開頭的地方結束,不跳過任何字元
>>> letters[::7]
'ahov'

>>> letters[::-1]
'zyxwvutsrqponmlkjihgfedcba'

>>> letters[::-2]
'zxvtrpnljhfdb'

results matching ""

    No results matching ""