1. 程式人生 > 其它 >fastjson轉換時有大括號或者冒號或者有中括號_[Python Basic] 字串處理以及型別轉換 1...

fastjson轉換時有大括號或者冒號或者有中括號_[Python Basic] 字串處理以及型別轉換 1...

技術標籤:fastjson轉換時有大括號或者冒號或者有中括號

String Manipulation & Typecasting (1)

1. 文字複製以及連線

1.1 Multiply sign

使用 multiply sigh/乘號* 來複制文字片段。

乘號複製文字舉例:
print("Hi" * 3) 
    # output: HiHiHi
print("*" * 10)
    # output:**********

1.2 連線

1.2.1 使用 plus sign 加號連線文字

加號連線文字舉例:

text1 = "I am"
text2 = "here" 
print(text1+ "空格" + text2)
    #output I am here

1.2.2使用 Comma sign 逗號連線文字

逗號連線文字舉例:

text1 = "I am"
text2 = "here" 
print(text1, text2)
    #output I am here
使用逗號和加號連線文字的區別在於,逗號連線文字在 python 中會自動加上空格

2.連線 string 字串

2.1 使用 f-string 連線字串

要列印字串,你也可以使用Python的字串格式,或者叫做 f-string。

英文:To print a string, you can also use Python's string formatting, or what is called f-strings.
f-string 輸出字串舉例:
print(f'Hello World') 
    #output: Hello World

2.1.1使用 f-string 連線變數

如果希望在字串文字中包含列印變數,只需將變數括在左大括號和右大括號中

英文:If you want to include printing a variable inside your string literal, you just need to enclose your variable within an open curly bracket, and a close curly bracket.
f-string 輸出變數舉例:

print(f'Hello World')
text1 = "I am"
text2 = "here"
print(f'Hi, {text1} {text2}') 
   # output : Hi, I am here
注意: f-string 中,print 函式中的空格會被實際輸出
使用 f-string, 能夠讓你的程式碼 優雅且可讀

3. 通過索引位置提取文字

通過引用文字從 LEFT 到 RIGHT 的索引位置,我們可以提取文字的任何部分

英文:We can extract any portion of a text, by >referencing to its index position from LEFT to >RIGHT.

如下我們將使用的例子:

hello jack 包含空格共 10 個字元位

58f7e99fbff6010a7dee917c42c6f7be.png

3.1 正序索引呼叫

使用左方括號和右方括號將索引括起來。 - 索引總是用一個左方括號和一個右方括號括起來。 - 從左側開始的第一個索引位置總是 index 0 而不是 1。

英文:Use the open and close square brackets to enclose the index. Indexes are always enclosed by an open and a close square bracket. The first index position from the LEFT is always index 0 and NOT 1.
例子:
text = "Hello Jack"
print(text[0])
    # output: H  
    ## 因為大寫 H 是字串"Hello Jack" 從左數第一個字元

print(text[4])
    # output: o  
    ## 同理小寫 o 是字串"Hello Jack" 從左數第4個字元

3.2 逆序索引呼叫

我們還可以通過"從右到左"引用文字的索引位置, 來提取文字的任何部分。 * 右邊第一個位置是index -1。

英文:We can also extract any portion of a text, by referencing its index position from RIGHT to left instead. * The first position from the RIGHT is index -1.
例子:
text = "Hello Jack"
print(text[-1])
    # output : k
    ## “k”是字串 Hello Jack 從右開始索引的第一個字元    

print(text[-9])
    # output : e
    ## “e”是字串 Hello Jack 從右開始索引的第 9 個字元

總結:

因此:正的索引位置從左開始,並從0、1、2、3、4、5 等開始, 而負的索引位置從右開始,並從-1、-2、-3、-4、-5、-6等開始.

英文: So, positive index positions start from the left, and start with 0, 1, 2, 3, 4, 5 and so on
while negative index positions start from the right, and start with -1, -2, -3 -4 -5, -6 and so on

3.3 提取部分字串

For the same string, you can either use the positive indexes or negative indexes.

3.3.1 如何調取前五個字元

只需表述為”:5“即可

To extract the first 5 characters, put a colon BEFORE the index.
例子:
text = "Hello Jack" 
print(text[:5])

    # output: Hello
    ##

3.3.2 如何提取後 3 個字元:

只需表述為:”-3:“

To extract the last 3 characters, put a colon AFTER the index, and use NEGATIVE
例子:
text = "Hello Jack" 
print(text[-3:])

    # output: ack
    ##

3.3.3 如何提取字串中的片段

我們還可以提取字串文字的片段,方法是先放入一個起始索引,後跟冒號,然後放入一個結束索引。例如:print(text[6:10])

We can also slice a string literal, by putting a >starting index, followed by the colon, and then >putting an ending index.
  • 注意,輸出包含起始索引,不包含結尾索引。 英文:Note that the starting index is inclusive, while the ending index is exclusive.

876266aca9e8a174885e260be20f9afe.png
正向提取例子:
text = "Hello Jack" 
print(text[6:10])

    # output: Jack
    ## 輸出文字6:10,將輸出Jack,
    因為起始索引位置6是J字元,結束索引位置 10-1 是k字元。
如上的例子 10-1 索引, 如果片段的結束索引數字是10,那麼意味 著片段中包含的最後一個索引位置數不是10,而是10-1,因為最後 一位索引位置是不包含在內的。
因此,請注意,在確定片段中包含的最後一個字元時,它始終是結束索引位置並減 1.
例如:
text1 = "Hello" 
print(text[2:4])

    # output: ll
    ## 實際輸出的是 第 2 到 3 位;即 4-1
再舉例一個長的 12 位的字串
text1 = "HelloTheWorld" 共 12 位置, 從 0 - 11 位 
print(text[8:12])

    # output: Worl
    ## 實際輸出的是 第 8 到 11 位;即 12-1

3.3.4 逆向提取片段

如果我們想通過從右向左索引來逆向提取片段,我們可以對索引使用負號,note! 同樣,起始索引是包含的,而結束索引是不包含在內的。

Now, if we want to slice by counting from RIGHT to left instead, we can use the NEGATIVE sign for the index. Note that again the starting index is inclusive, while the ending index is exclusive.

7b42e4d604faa4faa3afdcb9ffcff99b.png
逆向索引提取例子:
text = "Hello Jack" 
print(text[-6:-9])

    # output: ell
    ## 如上圖所示:輸出的是 -9 到 -7 的內容, 即 -9對應字母 e, -6減去 1 等於 -7 對應字母 l.

3.3.5 區域提取

我們還可以通過只放一個起始索引,後跟冒號,而不放結束索引來分割字串文字

We can also slice a string literal, by putting only a starting index, followed by the colon, and NOT putting an ending index

b354cbfd79e4b0a46cbdadbaccf8c52f.png

``` 正向區域索引提取例子: text = "Hello Jack" print(text[4:])

# output: o Jack
## 如上圖所示: 從左往右索引,因為起始索引位置4是 o 字元,而且由於我們不放結束索引,所以它將包含字串文字的最後一個字元,即 k 字元。
![](media/15783570801942/15816953873062.jpg)

逆向區域索引提取例子: text = "Hello Jack" print(text[-4:])

# output: Jack
## 如上圖所示,因為起始索引位置 -4 是 J 字元,而且由於們不放結束索引,所以它將包含直到字串的最後一個字元,即 k 字元。

```

完畢, 待總結。

詞彙:

  1. multiply sign 乘號
  2. concatenate text to join them together 翻譯:將文字連線在一起
  3. open and close curly brackets 大括號
  4. square bracket 方括號

釋出時間: 2020 年 2 月 15日

知乎連結: 字串處理以及型別轉換 1

當前可任意轉載,轉載請儲存以上資訊即可。無需獲得授權.