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 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

當前可任意轉載,轉載請保存以上信息即可。無需獲得授權.

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/378868.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/378868.shtml
英文地址,請注明出處:http://en.pswp.cn/news/378868.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Java IdentityHashMap size()方法與示例

IdentityHashMap類的size()方法 (IdentityHashMap Class size() method) size() method is available in java.util package. size()方法在java.util包中可用。 size() method is used to return the size (i.e. How many key-value pair exists) of this IdentityHashMap. siz…

讀《深入分析Java Web技術內幕》

這里這本書的預讀章節,看完預讀部分,解答了一些疑惑,也相信這是一本夯實Java Web架構體系的好書。 HTTP協議解析 開發一般使用firefox的firebug調試,這的確是一個利器,HTTP的請求頭響應頭一目了然。 瀏覽器緩存機制 當…

windows mobile多國語言實現[轉]

介紹一種多國語言的實現辦法,這也是微軟推薦的方式,打開windows mobile下的windows目錄可以看到有很多以MUI為后綴名的文件,例如shellres.dll.0804.mui、shell.dll.0804.mui。。。。。。我們可以用eXeScope.exe或者resources hacker這樣的文件…

RTSP協議基本分析

目錄一、介紹二、RTSP與HTTP三、RTSP推流基本過程1、OPTION 查詢服務器端可用方法1.1、Client 請求1.2、Server 回復2、ANNOUNCE 發送媒體描述信息2.1、Client 請求2.2、Server 回復3、SETUP建立RTSP會話3.1、Client 請求(視頻流)3.2、Server 回復&#…

找取照片上的25個特征點,并保存結果

找取照片上的25個特征點,并保存結果 import numpy as np import cv2 from matplotlib import pyplot as plt img cv2.imread(E:\Python-workspace\OpenCV\OpenCV/water1.png,1)#第一個參數為選擇照片的路徑,注意照片路徑最后一個為正斜杠其他都為反斜杠…

nutsdb與mysql_分享下 nutsdb 單機 1 億、10 億數據實測

大家好, 想給大家分享下我最近為 nutsdb 做的數據測試。測試項目起因事情起因是這個 issue ,簡單說就是內存高了,不夠用了。可能很多人不知道 NutsDB。簡單介紹下,NutsDB 是我幾個月以前開源的一個 Go 語言編寫的內嵌型 KV 數據庫…

java 方法 示例_帶有示例的Java EnumSetSupplementOf()方法

java 方法 示例EnumSet類complementOf()方法 (EnumSet Class complementOf() method) complementOf() method is available in java.util package. clipartOf()方法在java.util包中可用。 complementOf() method is used to contain all the elements of this EnumSet that are…

在需要時開啟Perl新特性

從5.10開始,新特性必須開啟才能使用。Perl默認不啟用新特性保持向后兼容。 如果想啟用新特性,可以使用新的-E開關。打開所有的新特性。 % perl5.10.1 -E say.pl #開啟5.10.1 版本的所有新特性 在源代碼中使用 use 指令之后指定perl版本號就可以了。 use …

P2P技術詳解(一):NAT詳解——詳細原理、P2P簡介

目錄1. IPv4協議和NAT的由來2. NAT的工作模型和特點2.1、NAT的概念模型2.2、一對一的NAT2.3、一對多的NAT2.4、按照NAT端口映射方式分類2.4.1全錐形NAT2.4.2限制錐形NAT2.4.3端口限制錐形NAT2.4.4對稱型NAT3. NAT的限制與解決方案3.1、IP端到端服務模型3.2、NAT的弊端3.3、NAT穿…

決定孩子命運的八大關鍵問題

你可以不是天才,但你可以是天才的父母!樹立做父母正確的家庭教育觀念,為孩子建造一個良好的人生平臺,讓孩子有很好的人格修養,懂得做人,懂得成功的真正含義。簡單方便,容易操作,適合…

java calendar_Java Calendar internalGet()方法與示例

java calendar日歷類internalGet()方法 (Calendar Class internalGet() method) internalGet() method is available in java.util package. internalGet()方法在java.util包中可用。 internalGet() method is used to get the value of the given field(fi) of this Calendar …

顯示照片的二維直方圖

顯示照片的二維直方圖 import cv2 from matplotlib import pyplot as plt img cv2.imread(E:\Python-workspace\OpenCV\OpenCV/water1.png,1)#第一個參數為選擇照片的路徑,注意照片路徑最后一個為正斜杠其他都為反斜杠;第二個參數,其中1表示…

周五怎么表示 mysql_完美起航-MySQL找每個月最后一個星期五--函數定義與使用

數據庫作業有一道題是這樣子的:有一張名叫emp的表記錄員工信息,其中有如下字段 HIREDATE 表示員工被雇用的日期:然后問題是這樣的:q7.Show details of employee hiredates and the date of their first payday.(Paydays occur on…

要想能安心,必須先死心。

其實,不論是感情,還是學習、工作還是生活,不都是如此?曾經年少懷抱一個名校夢,如果高考不成功,那么你一定會選擇考研讓自己死一次心;小時候特別喜歡 某個職業,長大了你拋棄所有機會追…

silverlight學習總結【完】

以下內容是個人理解,不保證正確性。且假設使用C#,并且有一定的相關知識和XML基礎。 silverlight是什么,能做什么 silverlight用XAML來做前端界面,用.NET或者JS作為程序腳本支持,在瀏覽器內外運行的應用。可以認為和FLA…

P2P技術詳解(二):P2P中的NAT穿越(打洞)方案詳解

目錄1、內容概述2、反向鏈接技術:一種特殊的P2P場景(通信雙方中只有一方位于NAT設備之后)3、基于UDP協議的P2P打洞技術詳解3.1、原理概述3.2、典型P2P情景1: 兩客戶端位于同一NAT設備后面(即相同內網中)3.3…

Java Byte類的compareTo()方法和示例

簡短的類compareTo()方法 (Short class compareTo() method) compareTo() method is available in java.lang package. compareTo()方法在java.lang包中可用。 compareTo() method is used to check equality or inequality for this Byte object against the given Byte objec…

顯示照片的RGB直方圖

顯示照片的RGB直方圖 import numpy as np import cv2 as cv from matplotlib import pyplot as plt img cv.imread(E:\Python-workspace\OpenCV\OpenCV/BEYOND.png,1)#第一個參數為選擇照片的路徑,注意照片路徑最后一個為正斜杠其他都為反斜杠;第二個參…

OUT還開通博客!

現在哪有人還在玩博客哦,哎試試,記錄一下自己開發網站的點滴吧!轉載于:https://www.cnblogs.com/17say/archive/2013/02/18/2915125.html

網站V5的一些想法(轉)

V5即將到來,面對“全新”的V5,前端這塊自然也要借這次改版的機會,將我們前端的一些想法實踐到V5中去,實現一次跨越。 1 嘗試模塊化的代碼書寫(html、css等) 模塊化的目的是為了提高代碼的重用性、擴展性、可維護性 2 文件引用使用…