<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                字符串的內容的確不少,甚至都有點啰嗦了。但是,本節依然還要繼續,就是因為在編程實踐中,經常會遇到有關字符串的問題,而且也是很多初學者容易迷茫的。 ## [](https://github.com/qiwsir/StarterLearningPython/blob/master/109.md#字符串格式化輸出)字符串格式化輸出 什么是格式化?在維基百科中有專門的詞條,這么說的: > 格式化是指對磁盤或磁盤中的分區(partition)進行初始化的一種操作,這種操作通常會導致現有的磁盤或分區中所有的文件被清除。 不知道你是否知道這種“格式化”。顯然,此格式化非我們這里所說的,我們說的是字符串的格式化,或者說成“格式化字符串”,都可以,表示的意思就是: > 格式化字符串,是C、C++等程序設計語言printf類函數中用于指定輸出參數的格式與相對位置的字符串參數。其中的轉換說明(conversion specification)用于把隨后對應的0個或多個函數參數轉換為相應的格式輸出;格式化字符串中轉換說明以外的其它字符原樣輸出。 這也是來自維基百科的定義。在這個定義中,是用C語言作為例子,并且用了其輸出函數來說明。在python中,也有同樣的操作和類似的函數`print`,此前我們已經了解一二了。 如果將那個定義說的通俗一些,字符串格式化化,就是要先制定一個模板,在這個模板中某個或者某幾個地方留出空位來,然后在那些空位填上字符串。那么,那些空位,需要用一個符號來表示,這個符號通常被叫做占位符(僅僅是占據著那個位置,并不是輸出的內容)。 ~~~ >>> "I like %s" 'I like %s' ~~~ 在這個字符串中,有一個符號:`%s`,就是一個占位符,這個占位符可以被其它的字符串代替。比如: ~~~ >>> "I like %s" % "python" 'I like python' >>> "I like %s" % "Pascal" 'I like Pascal' ~~~ 這是較為常用的一種字符串輸出方式。 另外,不同的占位符,會表示那個位置應該被不同類型的對象填充。下面列出許多,供參考。不過,不用記憶,常用的只有`%s`和`%d`,或者再加上`%f`,其它的如果需要了,到這里來查即可。 | 占位符 | 說明 | | --- | --- | | %s | 字符串(采用str()的顯示) | | %r | 字符串(采用repr()的顯示) | | %c | 單個字符 | | %b | 二進制整數 | | %d | 十進制整數 | | %i | 十進制整數 | | %o | 八進制整數 | | %x | 十六進制整數 | | %e | 指數 (基底寫為e) | | %E | 指數 (基底寫為E) | | %f | 浮點數 | | %F | 浮點數,與上相同 | | %g | 指數(e)?或浮點數 (根據顯示長度) | | %G | 指數(E)或浮點數 (根據顯示長度) | 看例子: ~~~ >>> a = "%d years" % 15 >>> print a 15 years ~~~ 當然,還可以在一個字符串中設置多個占位符,就像下面一樣 ~~~ >>> print "Suzhou is more than %d years. %s lives in here." % (2500, "qiwsir") Suzhou is more than 2500 years. qiwsir lives in here. ~~~ 對于浮點數字的打印輸出,還可以限定輸出的小數位數和其它樣式。 ~~~ >>> print "Today's temperature is %.2f" % 12.235 Today's temperature is 12.23 >>> print "Today's temperature is %+.2f" % 12.235 Today's temperature is +12.23 ~~~ 注意,上面的例子中,沒有實現四舍五入的操作。只是截取。 關于類似的操作,還有很多變化,比如輸出格式要寬度是多少等等。如果看官在編程中遇到了,可以到網上查找。我這里給一個參考圖示,也是從網上抄來的。 [![](https://box.kancloud.cn/2015-09-07_55ed2e4fd9ce7.png)](https://github.com/qiwsir/StarterLearningPython/blob/master/1images/10901.png) 其實,上面這種格式化方法,常常被認為是太“古老”了。因為在python中還有新的格式化方法。 ~~~ >>> s1 = "I like {}".format("python") >>> s1 'I like python' >>> s2 = "Suzhou is more than {} years. {} lives in here.".format(2500, "qiwsir") >>> s2 'Suzhou is more than 2500 years. qiwsir lives in here.' ~~~ 這就是python非常提倡的`string.format()`的格式化方法,其中`{}`作為占位符。 這種方法真的是非常好,而且非常簡單,只需要將對應的東西,按照順序在format后面的括號中排列好,分別對應占位符`{}`即可。我喜歡的方法。 如果你覺得還不明確,還可以這樣來做。 ~~~ >>> print "Suzhou is more than {year} years. {name} lives in here.".format(year=2500, name="qiwsir") Suzhou is more than 2500 years. qiwsir lives in here. ~~~ 真的很簡潔,看成優雅。 其實,還有一種格式化的方法,被稱為“字典格式化”,這里僅僅列一個例子,如果看官要了解字典的含義,本教程后續會有的。 ~~~ >>> lang = "python" >>> print "I love %(program)s"%{"program":lang} I love python ~~~ 列舉了三種基本格式化的方法,你喜歡那種?我推薦:`string.format()` ## [](https://github.com/qiwsir/StarterLearningPython/blob/master/109.md#常用的字符串方法)常用的字符串方法 字符串的方法很多。可以通過dir來查看: ~~~ >>> dir(str) ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] ~~~ 這么多,不會一一介紹,要了解某個具體的含義和使用方法,最好是使用help查看。舉例: ~~~ >>> help(str.isalpha) Help on method_descriptor: isalpha(...) S.isalpha() -> bool Return True if all characters in S are alphabetic and there is at least one character in S, False otherwise. ~~~ 按照這里的說明,就可以在交互模式下進行實驗。 ~~~ >>> "python".isalpha() #字符串全是字母,應該返回True True >>> "2python".isalpha() #字符串含非字母,返回False False ~~~ ### [](https://github.com/qiwsir/StarterLearningPython/blob/master/109.md#split)split 這個函數的作用是將字符串根據某個分割符進行分割。 ~~~ >>> a = "I LOVE PYTHON" >>> a.split(" ") ['I', 'LOVE', 'PYTHON'] ~~~ 這是用空格作為分割,得到了一個名字叫做列表(list)的返回值,關于列表的內容,后續會介紹。還能用別的分隔嗎? ~~~ >>> b = "www.itdiffer.com" >>> b.split(".") ['www', 'itdiffer', 'com'] ~~~ ### [](https://github.com/qiwsir/StarterLearningPython/blob/master/109.md#去掉字符串兩頭的空格)去掉字符串兩頭的空格 這個功能,在讓用戶輸入一些信息的時候非常有用。有的朋友喜歡輸入結束的時候敲擊空格,比如讓他輸入自己的名字,輸完了,他來個空格。有的則喜歡先加一個空格,總做的輸入的第一個字前面應該空兩個格。 這些空格是沒用的。python考慮到有不少人可能有這個習慣,因此就幫助程序員把這些空格去掉。 方法是: * S.strip() 去掉字符串的左右空格 * S.lstrip() 去掉字符串的左邊空格 * S.rstrip() 去掉字符串的右邊空格 例如: ~~~ >>> b=" hello " #兩邊有空格 >>> b.strip() 'hello' >>> b ' hello ' ~~~ 特別注意,原來的值沒有變化,而是新返回了一個結果。 ~~~ >>> b.lstrip() #去掉左邊的空格 'hello ' >>> b.rstrip() #去掉右邊的空格 ' hello' ~~~ ### [](https://github.com/qiwsir/StarterLearningPython/blob/master/109.md#字符大小寫的轉換)字符大小寫的轉換 對于英文,有時候要用到大小寫轉換。最有名駝峰命名,里面就有一些大寫和小寫的參合。如果有興趣,可以來這里看[自動將字符串轉化為駝峰命名形式的方法](https://github.com/qiwsir/algorithm/blob/master/string_to_hump.md)。 在python中有下面一堆內建函數,用來實現各種類型的大小寫轉化 * S.upper() #S中的字母大寫 * S.lower() #S中的字母小寫 * S.capitalize() #首字母大寫 * S.isupper() #S中的字母是否全是大寫 * S.islower() #S中的字母是否全是小寫 * S.istitle() 看例子: ~~~ >>> a = "qiwsir,python" >>> a.upper() #將小寫字母完全變成大寫字母 'QIWSIR,PYTHON' >>> a #原數據對象并沒有改變 'qiwsir,python' >>> b = a.upper() >>> b 'QIWSIR,PYTHON' >>> c = b.lower() #將所有的小寫字母變成大寫字母 >>> c 'qiwsir,python' >>> a 'qiwsir,python' >>> a.capitalize() #把字符串的第一個字母變成大寫 'Qiwsir,python' >>> a #原數據對象沒有改變 'qiwsir,python' >>> b = a.capitalize() #新建立了一個 >>> b 'Qiwsir,python' >>> a = "qiwsir,github" #這里的問題就是網友白羽毛指出的,非常感謝他。 >>> a.istitle() False >>> a = "QIWSIR" #當全是大寫的時候,返回False >>> a.istitle() False >>> a = "qIWSIR" >>> a.istitle() False >>> a = "Qiwsir,github" #如果這樣,也返回False >>> a.istitle() False >>> a = "Qiwsir" #這樣是True >>> a.istitle() True >>> a = 'Qiwsir,Github' #這樣也是True >>> a.istitle() True >>> a = "Qiwsir" >>> a.isupper() False >>> a.upper().isupper() True >>> a.islower() False >>> a.lower().islower() True ~~~ 順著白羽毛網友指出的,再探究一下,可以這么做: ~~~ >>> a = "This is a Book" >>> a.istitle() False >>> b = a.title() #這樣就把所有單詞的第一個字母轉化為大寫 >>> b 'This Is A Book' >>> b.istitle() #判斷每個單詞的第一個字母是否為大寫 True ~~~ ### [](https://github.com/qiwsir/StarterLearningPython/blob/master/109.md#join拼接字符串)join拼接字符串 用“+”能夠拼接字符串,但不是什么情況下都能夠如愿的。比如,將列表(關于列表,后續詳細說,它是另外一種類型)中的每個字符(串)元素拼接成一個字符串,并且用某個符號連接,如果用“+”,就比較麻煩了(是能夠實現的,麻煩)。 用字符串的join就比較容易實現。 ~~~ >>> b 'www.itdiffer.com' >>> c = b.split(".") >>> c ['www', 'itdiffer', 'com'] >>> ".".join(c) 'www.itdiffer.com' >>> "*".join(c) 'www*itdiffer*com' ~~~ 這種拼接,是不是簡單呢?
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看