fastjson转换时有大括号或者冒号或者有中括号_[Python Basic] 字符串处理以及类型转换 1...
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 here1.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 World2.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 個字符位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 onwhile 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.
因此,請注意,在確定片段中包含的最后一個字符時,它始終是結束索引位置并減 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.逆向索引提取例子: 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``` 正向區域索引提取例子: text = "Hello Jack" print(text[4:])
# output: o Jack ## 如上圖所示: 從左往右索引,因為起始索引位置4是 o 字符,而且由于我們不放結束索引,所以它將包含字符串文字的最后一個字符,即 k 字符。 逆向區域索引提取例子: text = "Hello Jack" print(text[-4:])
# output: Jack ## 如上圖所示,因為起始索引位置 -4 是 J 字符,而且由于們不放結束索引,所以它將包含直到字符串的最后一個字符,即 k 字符。```
完畢, 待總結。
詞匯:
發布時間: 2020 年 2 月 15日
知乎鏈接: 字符串處理以及類型轉換 1
當前可任意轉載,轉載請保存以上信息即可。無需獲得授權.
總結
以上是生活随笔為你收集整理的fastjson转换时有大括号或者冒号或者有中括号_[Python Basic] 字符串处理以及类型转换 1...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 峰米T1可以接驳音响系统吗
- 下一篇: nutsdb与mysql_分享下 nut