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

direktori cari
Python是什么? Python 3 教程 Python3 基礎(chǔ)語(yǔ)法 編碼 Python3 基本數(shù)據(jù)類(lèi)型 Python解釋器 Python 注釋 Python 數(shù)字運(yùn)算 Python 字符串 Python 列表 Python 編程第一步 Python 條件控制 Python 循環(huán) Python 函數(shù) Python 數(shù)據(jù)結(jié)構(gòu) Python 模塊 Python 輸入和輸出 Python 錯(cuò)誤和異常 Python 類(lèi) Python 標(biāo)準(zhǔn)庫(kù)概覽 Python Hello World 實(shí)例 Python 數(shù)字求和 Python 平方根 Python 二次方程 Python 計(jì)算三角形的面積 Python 隨機(jī)數(shù)生成 Python 攝氏溫度轉(zhuǎn)華氏溫度 Python 交換變量 Python if 語(yǔ)句 Python 判斷字符串是否為數(shù)字 Python 判斷奇數(shù)偶數(shù) Python 判斷閏年 Python 獲取最大值函數(shù) Python 質(zhì)數(shù)判斷 Python 階乘實(shí)例 Python 九九乘法表 Python 斐波那契數(shù)列 Python 阿姆斯特朗數(shù) Python 十進(jìn)制轉(zhuǎn)二進(jìn)制、八進(jìn)制、十六進(jìn)制 Python ASCII碼與字符相互轉(zhuǎn)換 Python 最大公約數(shù)算法 Python 最小公倍數(shù)算法 Python 簡(jiǎn)單計(jì)算器實(shí)現(xiàn) Python 生成日歷 Python 使用遞歸斐波那契數(shù)列 Python 文件 IO Python 字符串判斷 Python 字符串大小寫(xiě)轉(zhuǎn)換 Python 計(jì)算每個(gè)月天數(shù) Python 獲取昨天日期 Python list 常用操作 Python3 實(shí)例
watak

Python 字符串


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

>>> '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)和其它特殊字符來(lái)準(zhǔn)確地表示。

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

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

以下使用 \n 來(lái)添加新行:

>>> '"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.

以下使用 反斜線(\) 來(lái)續(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))括起來(lái)。使用三引號(hào)時(shí),換行符不需要轉(zhuǎn)義,它們會(huì)包含在字符串中。以下的例子使用了一個(gè)轉(zhuǎn)義符,避免在最開(kāi)始產(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)被串連;上例的第一行也可以寫(xiě)成 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 語(yǔ)言一樣,字符串的第一個(gè)字符的索引為 0。沒(méi)有單獨(dú)的字符類(lèi)型;一個(gè)字符就是長(zhǎng)度為一的字符串。就像Icon編程語(yǔ)言一樣,子字符串可以使用分切符來(lái)指定:用冒號(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)容的方法來(lá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è)過(guò)大的索引將被字符串的大小取代,上限值小于下限值將返回一個(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ì)從右開(kāi)始計(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
Artikel sebelumnya: Artikel seterusnya: