Strings
自譯:字符串
Besides numbers, Python can also manipulate strings, which can be expressed in several ways. They can be enclosed in single quotes ('...') or double quotes ("...") with the same result 2. \ can be used to escape quotes:
自譯:除了數字,python還可以操作字符串,字符串可以用多種方式進行表達。使用單引號’string』或雙引號」string」標註的都是一樣的結果,」\」被用作轉義標註符。
In the interactive interpreter, the output string is enclosed in quotes and special characters are escaped with backslashes. While this might sometimes look different from the input (the enclosing quotes could change), the two strings are equivalent. The string is enclosed in double quotes if the string contains a single quote and no double quotes, otherwise it is enclosed in single quotes. The print() function produces a more readable output, by omitting the enclosing quotes and by printing escaped and special characters:
自譯:在交互解釋器中,輸出字符串使用引號擴起來,特殊字符是使用反斜槓轉義。因為轉義符號的原因,有時同輸入字符串相比是不同,但其實他們是相同的。法國字符串包含單引號並且沒有雙引號的號的話引用標註是使用雙引號,否則使用單引號。通過省略標註引號並且列印轉義和特殊字符串的方式,列印功能可以提供易讀性更強的輸出。
If you don’t want characters prefaced by \ to be interpreted as special characters, you can use raw strings by adding an r before the first quote:
自譯:如果你不想使用轉義符去標註特殊字符串,你可以在第一個字母前加」r」標註字符串
String literals can span multiple lines. One way is using triple-quotes: """...""" or '''...'''. End of lines are automatically included in the string, but it’s possible to prevent this by adding a \ at the end of the line. The following example:
自譯:字符串可以跨行。一種方式是使用」」」」」」或者』』』』』』符號進行標註。多行行尾將自動標註在引號內。但也可以通過在行尾添加」\」來防止行尾出現這種情況。例如:
produces the following output (note that the initial newline is not included):
自譯:生成以下輸出(注意不包含初始和結束的換行)
Strings can be concatenated (glued together) with the + operator, and repeated with *:
自譯:字符串可以使用」+」進行拼接操作,並用」*」替代重複,例如:
Two or more string literals (i.e. the ones enclosed between quotes) next to each other are automatically concatenated.
自譯:相鄰的兩個或更多字符串文字會自動拼接在一起(即引號之間的字符串文字)。
This feature is particularly useful when you want to break long strings:
自譯:當你想斷開長的字符串時,這個功能尤其方便易用。
This only works with two literals though, not with variables or expressions:
自譯:這種操作只適合兩個字符串文本,不適用於變量和表達式:
If you want to concatenate variables or a variable and a literal, use +:
自譯:如果你想拼接變量或一個變量和字符串文字,需要使用」+』
Strings can be indexed (subscripted), with the first character having index 0. There is no separate character type; a character is simply a string of size one:
自譯:字符串可以被索引(下標)。起始索引位置為0、並沒有單獨的字符串文字類型;一個字符串文字只是長度為1的字符串文字。
Indices may also be negative numbers, to start counting from the right:
自譯:索引也可以是負數,從右側開始計算:
Note that since -0 is the same as 0, negative indices start from -1.
自譯:需要注意的是」-0」和」0」是相同的,負數索引是從」-1」開始的。
In addition to indexing, slicing is also supported. While indexing is used to obtain individual characters, slicing allows you to obtain substring:
自譯:除了索引,切片也是被支持的。當索引被用於獲取字符串中單個字符時,切片允許你通過使用索引下標的方式去操作。
Note how the start is always included, and the end always excluded. This makes sure that s[:i] + s[i:] is always equal to s:
自譯:需要注意的是使用索引時為」含左不含右」。這可以確保通過使用索引拼合的字符串和原字符串相等」s[:i] + s[i:] = s」。
Slice indices have useful defaults; an omitted first index defaults to zero, an omitted second index defaults to the size of the string being sliced.
自譯:切片索引包含一些默認值;左側為空時默認起始位置為0,右側為空時默認結束位置為字符串的長度值。
One way to remember how slices work is to think of the indices as pointing between characters, with the left edge of the first character numbered 0. Then the right edge of the last character of a string of n characters has index n, for example:
自譯:理解切片是如何工作的一種方式是想清楚切片指向字符之間,第一個字符串字符的左邊緣編號為0.字符串結束字符的右側編號為n,例如:
The first row of numbers gives the position of the indices 0…6 in the string; the second row gives the corresponding negative indices. The slice from i to j consists of all characters between the edges labeled i and j, respectively.
例如:在字符串中第一行數字給出了索引0-6對應的字符串字符;第二行給出負數索引-1 -- -6對應字符串字符。從i到j的切片包含了從i到j邊緣之間的所有字符串字符。
For non-negative indices, the length of a slice is the difference of the indices, if both are within bounds. For example, the length of word[1:3] is 2.
自譯:對於非負數索引,如果兩個索引都在範圍之內,那麼切片的長度是索引的差值。例如字符串word的切片索引word[1:3]的長度是3-1即2。
Attempting to use an index that is too large will result in an error:
自譯:如果嘗試使用過大的索引書,那麼會導致超出索引位置的報錯。
However, out of range slice indexes are handled gracefully when used for slicing:
自譯:然而,對於切片索引而言,超出索引值的話會被做相關容錯處理。
Python strings cannot be changed — they are immutable. Therefore, assigning to an indexed position in the string results in an error:
自譯:python字符串是不能被改變的--它們是不可更改的。因此,分配給相應索引位一個字符會得到一個報錯的結果。
If you need a different string, you should create a new one:
自譯:如果你需要一個特殊字符串,你應該創建一個新的字符串。
The built-in function len() returns the length of a string:
自譯:內置函數」len」可以給出字符串的長度值。
See also
自譯:另見備註
Python3.9官方文檔翻譯版python簡介之數字
Python3.9官方文檔翻譯版之解釋器的使用1
Python3.9官方文檔翻譯版之解釋器的使用2
Python3.9官方文檔翻譯版之篇首語Python3.9官方文檔翻譯版之摘要部分