Python3.9官方文檔翻譯版python簡介之字符串

2020-12-14 老王說侃

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官方文檔翻譯版之摘要部分

相關焦點

  • Python3.9官方文檔翻譯版python簡介之列表
    To avoid this and get 9, you can use (-3)**2.自譯:由於」**」的優先級高於」-」,所以」-3**2」的運算結果為」-9」,」 (-3)**2」的運算結果為」9」。
  • Python3.9官方文檔翻譯版python簡介之數字
    在python中注釋是以#開頭並延續到本行結束。一個注釋通常出現在一行的開頭或者接下來的物理行或代碼結尾,但不能出現在字符串中。出現在字符串中的」#」僅僅是一個字符。由於注釋是解釋代碼功能,所以它並不能被python進行編譯使用,在輸入示例中時也可以不輸入它們。
  • Python入門之字符串基本操作(Python官方文檔翻譯)
    雖然這可能有時看起來不同於輸入(封閉引用可以改變),但這兩個字符串是等價的。如果字符串包含單個引號而沒有雙引號,則字符串以雙引號括起來,否則將以單引號括起來。print( )函數通過省略封閉引用和列印轉義字符和特殊字符來生成更可讀的輸出:>> '"Isn\'t," she said.'
  • [快學Python3]String(字符串)
    下面我們如何用不同的引號來創建字符串:a = u'我是字符串'b = u"我是字符串"c = """我是字符串我是字符串我還是字符串"""由上面看出,三引號括起來的字符串可以有多行。字符串內置函數在python中字符串內置函數提供了各種字符串處理能力,下面我們看幾個常用的函數應用示例:字符連接和切割在python中提供了以下函數完成連接和切割功能:下面我們看看具體的代碼示例:
  • 【Python】Python入門-字符串初相識
    公眾號:尤而小屋作者:Peter編輯:Peter從本文開始準備介紹Python中的常見數據結構:字符串、列表、集合、字典。其中字符串、列表、字典應用非常頻繁,需要重點掌握,本文介紹的是字符串及相關操作和方法。最後的字符串3種格式化方法將在下篇文章詳細講解。
  • Python兩種基本數據類型:數值及字符串;python交互式運行環境
    裡面用到很多python編程的知識點。筆者覺得有必要解釋一下。編程需要大量數據,為了編程方便,數據就分成了不同的類型,不同的數據類型有不同的特性。Python兩種基本數據類型python語言中,有兩種最基本的數據類型:數值類型及字符串類型。
  • [編程基礎] Python字符串替換筆記
    1.replace方法(常用)2.translate方法3.re.sub方法4.字符串切片(根據Python字符串切片方法替換字符)1 replace方法Python replace方法把字符串中的old(舊字符串) 替換成new(新字符串),如果指定第三個參數max,則設置替換次數不超過 max 次。
  • python字符編碼和字符串【小白入門學習必備知識點】
    字符編碼的作用就是將人類可識別的字符轉換為機器可識別的字節碼,以及反向過程。UNICDOE才是真正的字符串,而用ASCII、UTF-8、GBK等字符編碼表示的是字節串。關於這點,我們可以在Python的官方文檔中經常可以看到這樣的描述「Unicode string」 , 「translating a Unicode string into a sequence of bytes」。
  • 《Python入門篇》—字符串初相識
    從本文開始準備介紹Python中的常見數據結構:字符串、列表、集合、字典。其中字符串、列表、字典應用非常頻繁,需要重點掌握,本文介紹的是字符串及相關操作和方法。最後的字符串3種格式化方法將在下篇文章詳細講解。
  • python中字符串插值替換
    python string interpolation,我在這裡翻譯成字符串插值替換。
  • Python列表與字符串
    示例如下a = [1, 2, 3]  # 定義列表b = (4, 5, 6)  # 定義元組1.1.2 列表拼接使用+可以將兩個列表組成一個字符串>>> a = [1, 2, 3]
  • Python3.9官方文檔翻譯版之摘要部分
    學習程式語言,很多彎路可走,有一條路可能不是捷徑但一定不是彎路,那就是官方文檔的閱讀與消化!Python is an easy to learn, powerful programming language.
  • 詳解Python拼接字符串的七種方式
    幾乎任何一種程式語言,都把字符串列為最基礎和不可或缺的數據類型。而拼接字符串是必備的一種技能。今天,我跟大家一起來學習Python拼接字符串的七種方式。上例中,簡潔版的花括號中無內容,缺點是容易弄錯次序。對號入座版主要有兩種,一種傳入序列號,一種則使用key-value的方式。實戰中,我們更推薦後一種,既不會數錯次序,又更直觀可讀。
  • Python拼接字符串的七種方式
    公眾號Python貓, 專注python技術、數據科學和深度學習,力圖創造一個有趣又有用的學習分享平臺。忘了在哪看到一位編程大牛調侃,他說程式設計師每天就做兩件事,其中之一就是處理字符串。相信不少同學會有同感。幾乎任何一種程式語言,都把字符串列為最基礎和不可或缺的數據類型。而拼接字符串是必備的一種技能。今天,我跟大家一起來學習Python拼接字符串的七種方式。
  • Python 字符串格式化方法總結
    比如日誌文件,設置好格式,後期在查詢問題時,就可以快速定位。字符串格式化就類似於手機APP界面一樣,UI、排版設計都是為了使人機互動更加直觀,內容讓人看起來更美觀、更舒服。c = a / bprint(f'百分數為:{c:%}')#百分數為:33.333333%print(f'百分數保留兩位小數為:{c:.2%}')#百分數保留兩位小數為:33.33%格式化 datetime 對象支持的格式詳見官方文檔:https://docs.python.org/3/library/datetime.html
  • 詳解 Python 拼接字符串的 7 種方式
    幾乎任何一種程式語言,都把字符串列為最基礎和不可或缺的數據類型。而拼接字符串是必備的一種技能。今天,我跟大家一起來學習Python拼接字符串的七種方式。上例中,簡潔版的花括號中無內容,缺點是容易弄錯次序。對號入座版主要有兩種,一種傳入序列號,一種則使用key-value的方式。實戰中,我們更推薦後一種,既不會數錯次序,又更直觀可讀。
  • Python3.6新特性官方文檔中文版
    另請參閱PEP 494 - Python 3.6 發布時間表摘要 - 發布亮點新的語法特性:PEP 498, 格式化字符串字面量PEP當用戶沒有指定版本(通過命令行參數或配置文件)時,py.exe啟動器以交互方式使用時,不再以Python 2優先於Python 3。 處理shebang行的方式保持不變 - 此處的「python」依舊指Python 2。python.exe和pythonw.exe已標記為長路徑敏感(long-path aware),這意味著260字符路徑限制可能不再適用。
  • python格式化字符串研究
    前言與隊友交流時提及python的格式化字符串漏洞,這個漏洞之前接觸不多,所以寫篇文章從基礎部分仔細研究了研究。python環境是python3.7。Python3裡的格式化字符串python3中的格式化字符串主要有以下兩種形式:"test %s" % ('test')"test {0}".format('test')這兩個語句的輸出都是test test。
  • Python數據類型之字符串
    # 字符串是python中最常用的數據類型,我們可以使用引號來創建字符串,引號可以是單引號,雙引號或三引號,本質上單引號和雙引號以及三引號的作用是一樣的 # 字符串是一種不可變的序列類型,單個字符從左到右按照順序排列,同時修改某個位置的字符是不被允許的 #
  • Python字符串(Python學習筆記02)
    字符串Python 3 中的字符串可以使用雙引號或單引號標示,如果字符串出現引號,則可以使用 來去除引號標示字符串的作用。幾種字符串的表示方法:str1 = "hello"str2 = 'world'str3 = 'hello, 'world''str4 = "hello, 'world'"str5 = 'hello, "world"'注意 str4 和 str5 都沒有使用 ,但仍然可以在字符串中使用引號。