天天看點

Python3 與 C# 基礎文法對比(String專欄)

Code:https://github.com/lotapp/BaseCode

多圖舊排版:https://www.cnblogs.com/dunitian/p/9119986.html

線上程式設計:https://mybinder.org/v2/gh/lotapp/BaseCode/master

線上預覽:http://github.lesschina.com/python/base/pop/2.str.html

Python設計的目的就是 ==> 讓程式員解放出來,不要過于關注代碼本身

步入正題:歡迎提出更簡單或者效率更高的方法

基礎系列:(這邊重點說說<code>Python</code>,上次講過的東西我就一筆帶過了)

In [1]:

In [2]:

重點說下<code>python</code>的 下标,有點意思,最後一個元素,我們一般都是<code>len(str)-1</code>,他可以直接用<code>-1</code>,倒2自然就是<code>-2</code>了

最後一個元素:<code>user_str[-1]</code>

user_str[-1]

user_str[len(user_str)-1] #其他程式設計語言寫法

倒數第二個元素:<code>user_str[-2]</code>

user_str[len(user_str)-2] #其他程式設計語言寫法

In [3]:

In [4]:

In [5]:

In [6]:

Out[6]:

In [7]:

In [8]:

python切片文法:<code>[start_index:end_index:step]</code> (end_index取不到)

eg:<code>str[1:4]</code> 取str[1]、str[2]、str[3]

eg:<code>str[2:]</code> 取下标為2開始到最後的元素

eg:<code>str[2:-1]</code> 取下标為2~到倒數第二個元素(end_index取不到)

eg:<code>str[1:6:2]</code> 隔着取~str[1]、str[3]、str[5](案例會詳細說)

eg:<code>str[::-1]</code> 逆向輸出(案例會詳細說)

In [9]:

In [10]:

In [11]:

In [12]:

In [13]:

這次為了更加形象對比,一句一句翻譯成C#

有沒有發現規律,<code>user_str[user_str.Length-1]</code>==&gt; -1是最後一個

<code>user_str[user_str.Length-2]</code>==&gt; -2是最後第二個

python的切片其實就是在這方面簡化了

其實你用<code>Pytho</code>n跟其他語言對比反差更大,<code>net</code>真的很強大了。

補充(對比看就清楚<code>Python</code>的<code>step</code>為什麼是2了,i+=2==&gt;2)

<code>find</code>,<code>rfind</code>,<code>index</code>,<code>rindex</code>

Python查找 推薦你用<code>find</code>和<code>rfind</code>

python:<code>xxx.count(str, start, end)</code>

Python:<code>xxx.replace(str1, str2, 替換次數)</code>

<code>split</code>(按指定字元分割),<code>splitlines</code>(按行分割)

<code>partition</code>(以str分割成三部分,str前,str和str後),<code>rpartition</code>(從右邊開始)

說下 split的切片用法:<code>print(test_input.split(" ",3))</code> 在第三個空格處切片,後面的不切了

繼續說說<code>splitlines</code>(按行分割),和<code>split("\n")</code>的差別:

擴充:<code>split()</code>,預設按 空字元切割(<code>空格、\t、\n</code>等等,不用擔心傳回<code>''</code>)

最後說一下<code>partition</code>和<code>rpartition</code>: 傳回是元祖類型(後面會說的)

方式和find一樣,找到第一個比對的就罷工了【注意一下沒找到的情況】

In [14]:

join :<code>"-".join(test_list)</code>

In [15]:

<code>startswith</code>(以。。。開頭),<code>endswith</code>(以。。。結尾)

In [16]:

<code>lower</code>(字元串轉換為小寫),<code>upper</code>(字元串轉換為大寫)

<code>title</code>(單詞首字母大寫),<code>capitalize</code>(第一個字元大寫,其他變小寫)

In [17]:

<code>lstrip</code>(去除左邊空格),<code>rstrip</code>(去除右邊空格)

<code>strip</code> (去除兩邊空格)美化輸出系列:<code>ljust</code>,<code>rjust</code>,<code>center</code>

<code>ljust,rjust,center</code>這些就不說了,python經常在linux終端中輸出,是以這幾個用的比較多

In [18]:

<code>isalpha</code>(是否是純字母),<code>isalnum</code>(是否是數字|字母)

<code>isdigit</code>(是否是純數字),<code>isspace</code>(是否是純空格)

注意~ <code>test_str5=" \t \n "</code> # isspace() ==&gt;true

In [19]:

In [20]:

Out[20]:

In [21]:

Out[21]:

In [22]:

Out[22]:

In [23]:

Out[23]:

In [24]:

Out[24]:

In [25]:

Out[25]:

In [26]:

Out[26]:

In [27]:

Out[27]:

In [28]:

Out[28]:

像這些方法練習用<code>ipython3</code>就好了(<code>sudo apt-get install ipython3</code>)

code的話需要一個個的print,比較麻煩(我這邊因為需要寫文章,是以隻能一個個code)

Python3 與 C# 基礎文法對比(String專欄)

<code>index0f</code>就相當于python裡面的<code>find</code>

<code>LastIndexOf</code> ==&gt; <code>rfind</code>

In [29]:

這個真用基礎來解決的話,兩種方法:

第一種自己變形一下:(原字元串長度 - 替換後的長度) / 字元串長度

字元串統計另一種方法(就用index)

替換指定次數的功能有點業餘,就不說了,你可以自行思考哦~

In [32]:

<code>split</code>裡面很多重載方法,可以自己去檢視下

eg:<code>Split("\n",StringSplitOptions.RemoveEmptyEntries)</code>

再說一下這個:<code>test_str.Split('a');</code> //傳回數組

如果要和Python一樣傳回清單==》<code>test_str.Split('a').ToList();</code> 【需要引用linq的命名空間哦】

<code>string.Join(分隔符,數組)</code>

<code>StartsWith</code>(以。。。開頭),<code>EndsWith</code>(以。。。結尾)

In [35]:

<code>Tirm</code>很強大,除了去空格還可以去除你想去除的任意字元

net裡面<code>string.Format</code>各種格式化輸出,可以參考,這邊就不講了

In [36]:

<code>string.IsNullOrEmpty</code> 和 <code>string.IsNullOrWhiteSpace</code> 是系統自帶的

In [37]:

其他的你需要自己封裝一個擴充類(eg:簡單封裝)

作者:毒逆天

出處:https://www.cnblogs.com/dotnetcrazy

打賞:<b>18i4JpL6g54yAPAefdtgqwRrZ43YJwAV5z</b>

本文版權歸作者和部落格園共有。歡迎轉載,但必須保留此段聲明,且在文章頁面明顯位置給出原文連接配接!