亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

目錄 搜索
文字

Python 字符串


除了數(shù)字,Python也能操作字符串。字符串有幾種表達(dá)方式,可以使用單引號(hào)或雙引號(hào)括起來:

>>> 'spam eggs'
'spam eggs'
>>> 'doesn\'t'
"doesn't"
>>> "doesn't"
"doesn't"
>>> '"Yes," he said.'
'"Yes," he said.'
>>> "\"Yes,\" he said."
'"Yes," he said.'
>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'

Python中使用反斜杠轉(zhuǎn)義引號(hào)和其它特殊字符來準(zhǔn)確地表示。

如果字符串包含有單引號(hào)但不含雙引號(hào),則字符串會(huì)用雙引號(hào)括起來,否則用單引號(hào)括起來。對(duì)于這樣的輸入字符串,print() 函數(shù)會(huì)產(chǎn)生更易讀的輸出。

跨行的字面字符串可用以下幾種方法表示。使用續(xù)行符,即在每行最后一個(gè)字符后使用反斜線來說明下一行是上一行邏輯上的延續(xù):

以下使用 \n 來添加新行:

>>> '"Isn\'t," she said.'
'"Isn\'t," she said.'
>>> print('"Isn\'t," she said.')
"Isn't," she said.
>>> s = 'First line.\nSecond line.'  # \n 意味著新行
>>> s  # 不使用 print(), \n 包含在輸出中
'First line.\nSecond line.'
>>> print(s)  # 使用 print(), \n 輸出一個(gè)新行
First line.
Second line.

以下使用 反斜線(\) 來續(xù)行:

hello = "This is a rather long string containing\n\
several lines of text just as you would do in C.\n\
    Note that whitespace at the beginning of the line is\
 significant."

print(hello)

注意,其中的換行符仍然要使用 \n 表示——反斜杠后的換行符被丟棄了。以上例子將如下輸出:

This is a rather long string containing
several lines of text just as you would do in C.
    Note that whitespace at the beginning of the line is significant.

或者,字符串可以被 """ (三個(gè)雙引號(hào))或者 ''' (三個(gè)單引號(hào))括起來。使用三引號(hào)時(shí),換行符不需要轉(zhuǎn)義,它們會(huì)包含在字符串中。以下的例子使用了一個(gè)轉(zhuǎn)義符,避免在最開始產(chǎn)生一個(gè)不需要的空行。

print("""\
Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to
""")

其輸出如下:

Usage: thingy [OPTIONS]
     -h                        Display this usage message
     -H hostname               Hostname to connect to

如果我們使用"原始"字符串,那么 \n 不會(huì)被轉(zhuǎn)換成換行,行末的的反斜杠,以及源碼中的換行符,都將作為數(shù)據(jù)包含在字符串內(nèi)。例如:

hello = r"This is a rather long string containing\n\
several lines of text much as you would do in C."

print(hello)

將會(huì)輸出:

This is a rather long string containing\n\
several lines of text much as you would do in C.

字符串可以使用 + 運(yùn)算符串連接在一起,或者用 * 運(yùn)算符重復(fù):

>>> word = 'Help' + 'A'
>>> word
'HelpA'
>>> '<' + word*5 + '>'
'<HelpAHelpAHelpAHelpAHelpA>'

兩個(gè)緊鄰的字面字符串將自動(dòng)被串連;上例的第一行也可以寫成 word = 'Help' 'A' ;這樣的操作只在兩個(gè)字面值間有效,不能隨意用于字符串表達(dá)式中:

>>> 'str' 'ing'                   #  <-  這樣操作正確
'string'
>>> 'str'.strip() + 'ing'   #  <-  這樣操作正確
'string'
>>> 'str'.strip() 'ing'     #  <-  這樣操作錯(cuò)誤
  File "<stdin>", line 1, in ?
    'str'.strip() 'ing'
                      ^
SyntaxError: invalid syntax

字符串可以被索引;就像 C 語言一樣,字符串的第一個(gè)字符的索引為 0。沒有單獨(dú)的字符類型;一個(gè)字符就是長(zhǎng)度為一的字符串。就像Icon編程語言一樣,子字符串可以使用分切符來指定:用冒號(hào)分隔的兩個(gè)索引。

>>> word[4]
'A'
>>> word[0:2]
'Hl'
>>> word[2:4]
'ep'

默認(rèn)的分切索引很有用:默認(rèn)的第一個(gè)索引為零,第二個(gè)索引默認(rèn)為字符串可以被分切的長(zhǎng)度。

>>> word[:2]    # 前兩個(gè)字符
'He'
>>> word[2:]    # 除了前兩個(gè)字符之外,其后的所有字符
'lpA'

不同于C字符串的是,Python字符串不能被改變。向一個(gè)索引位置賦值會(huì)導(dǎo)致錯(cuò)誤:

>>> word[0] = 'x'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'str' object does not support item assignment
>>> word[:1] = 'Splat'
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: 'str' object does not support slice assignment

然而,用組合內(nèi)容的方法來創(chuàng)建新的字符串是簡(jiǎn)單高效的:

>>> 'x' + word[1:]
'xelpA'
>>> 'Splat' + word[4]
'SplatA'
在分切操作字符串時(shí),有一個(gè)很有用的規(guī)律: s[:i] + s[i:] 等于 s.

>>> word[:2] + word[2:]
'HelpA'
>>> word[:3] + word[3:]
'HelpA'

對(duì)于有偏差的分切索引的處理方式也很優(yōu)雅:一個(gè)過大的索引將被字符串的大小取代,上限值小于下限值將返回一個(gè)空字符串。

>>> word[1:100]
'elpA'
>>> word[10:]

>>> word[2:1]

在索引中可以使用負(fù)數(shù),這將會(huì)從右往左計(jì)數(shù)。例如:

>>> word[-1]     # 最后一個(gè)字符
'A'
>>> word[-2]     # 倒數(shù)第二個(gè)字符
'p'
>>> word[-2:]    # 最后兩個(gè)字符
'pA'
>>> word[:-2]    # 除了最后兩個(gè)字符之外,其前面的所有字符
'Hel'
但要注意, -0 和 0 完全一樣,所以 -0 不會(huì)從右開始計(jì)數(shù)!

>>> word[-0]     # (既然 -0 等于 0)
'H'

超出范圍的負(fù)數(shù)索引會(huì)被截去多余部分,但不要嘗試在一個(gè)單元素索引(非分切索引)里使用:

>>> word[-100:]
'HelpA'
>>> word[-10]    # 錯(cuò)誤
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
IndexError: string index out of range

有一個(gè)方法可以讓您記住分切索引的工作方式,想像索引是指向字符之間,第一個(gè)字符左邊的數(shù)字是 0。接著,有n個(gè)字符的字符串最后一個(gè)字符的右邊是索引n,例如:

 +---+---+---+---+---+
 | H | e | l | p | A |
 +---+---+---+---+---+
 0   1   2   3   4   5
-5  -4  -3  -2  -1

第一行的數(shù)字 0...5 給出了字符串中索引的位置;第二行給出了相應(yīng)的負(fù)數(shù)索引。分切部分從 i 到 j 分別由在邊緣被標(biāo)記為 i 和 j 的全部字符組成。

對(duì)于非負(fù)數(shù)分切部分,如果索引都在有效范圍內(nèi),分切部分的長(zhǎng)度就是索引的差值。例如, word[1:3] 的長(zhǎng)度是2。

內(nèi)置的函數(shù) len() 用于返回一個(gè)字符串的長(zhǎng)度:

>>> s = 'supercalifragilisticexpialidocious'
>>> len(s)
34
上一篇: 下一篇: