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

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                我們有兩種大相徑庭地輸出值方法:表達式語句和?print()?函數(第三種訪求是使用文件對象的write()?方法,標準文件輸出可以參考?sys.stdout,詳細內容參見庫參考手冊)。 通常,你想要對輸出做更多的格式控制,而不是簡單的打印使用空格分隔的值。有兩種方法可以格式化你的輸出:第一種方法是由你自己處理整個字符串,通過使用字符串切割和連接操作可以創建任何你想要的輸出形式。string 類型包含一些將字符串填充到指定列寬度的有用操作,隨后就會討論這些。第二種方法是使用?str.format()?方法。 標準模塊?string?包括了一些操作,將字符串填充入給定列時,這些操作很有用。隨后我們會討論這部分內容。第二種方法是使用?Template?方法。 當然,還有一個問題,如何將值轉化為字符串?很幸運,Python 有辦法將任意值轉為字符串:將它傳入?repr()?或?str()?函數。 函數?str()?用于將值轉化為適于人閱讀的形式,而?repr()?轉化為供解釋器讀取的形式(如果沒有等價的語法,則會發生?SyntaxError?異常)某對象沒有適于人閱讀的解釋形式的話,str()?會返回與?repr()?等同的值。很多類型,諸如數值或鏈表、字典這樣的結構,針對各函數都有著統一的解讀方式。字符串和浮點數,有著獨特的解讀方式。 下面有些例子: ~~~ >>> s = 'Hello, world.' >>> str(s) 'Hello, world.' >>> repr(s) "'Hello, world.'" >>> str(1/7) '0.14285714285714285' >>> x = 10 * 3.25 >>> y = 200 * 200 >>> s = 'The value of x is ' + repr(x) + ', and y is ' + repr(y) + '...' >>> print(s) The value of x is 32.5, and y is 40000... >>> # The repr() of a string adds string quotes and backslashes: ... hello = 'hello, world\n' >>> hellos = repr(hello) >>> print(hellos) 'hello, world\n' >>> # The argument to repr() may be any Python object: ... repr((x, y, ('spam', 'eggs'))) "(32.5, 40000, ('spam', 'eggs'))" ~~~ 有兩種方式可以寫平方和立方表: ~~~ >>> for x in range(1, 11): ... print(repr(x).rjust(2), repr(x*x).rjust(3), end=' ') ... # Note use of 'end' on previous line ... print(repr(x*x*x).rjust(4)) ... 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 >>> for x in range(1, 11): ... print('{0:2d} {1:3d} {2:4d}'.format(x, x*x, x*x*x)) ... 1 1 1 2 4 8 3 9 27 4 16 64 5 25 125 6 36 216 7 49 343 8 64 512 9 81 729 10 100 1000 ~~~ (注意第一個例子,print?在每列之間加了一個空格,它總是在參數間加入空格。) 以上是一個?str.rjust()?方法的演示,它把字符串輸出到一列,并通過向左側填充空格來使其右對齊。類似的方法還有?str.ljust()?和?str.center()。這些函數只是輸出新的字符串,并不改變什么。如果輸出的字符串太長,它們也不會截斷它,而是原樣輸出,這會使你的輸出格式變得混亂,不過總強過另一種選擇(截斷字符串),因為那樣會產生錯誤的輸出值(如果你確實需要截斷它,可以使用切割操作,例如:x.ljust(n)[:n]?)。 還有另一個方法,?str.zfill()?它用于向數值的字符串表達左側填充 0。該函數可以正確理解正負號: ~~~ >>> '12'.zfill(5) '00012' >>> '-3.14'.zfill(7) '-003.14' >>> '3.14159265359'.zfill(5) '3.14159265359' ~~~ 方法?str.format()?的基本用法如下: ~~~ >>> print('We are the {} who say "{}!"'.format('knights', 'Ni')) We are the knights who say "Ni!" ~~~ 大括號和其中的字符會被替換成傳入?str.format()?的參數。大括號中的數值指明使用傳入str.format()?方法的對象中的哪一個: ~~~ >>> print('{0} and {1}'.format('spam', 'eggs')) spam and eggs >>> print('{1} and {0}'.format('spam', 'eggs')) eggs and spam ~~~ 如果在?str.format()?調用時使用關鍵字參數,可以通過參數名來引用值: ~~~ >>> print('This {food} is {adjective}.'.format( ... food='spam', adjective='absolutely horrible')) This spam is absolutely horrible. ~~~ 定位和關鍵字參數可以組合使用: ~~~ >>> print('The story of {0}, {1}, and {other}.'.format('Bill', 'Manfred', other='Georg')) The story of Bill, Manfred, and Georg. ~~~ '!a'?(應用?ascii()),'!s'?(應用?str()?)和?'!r'?(應用?repr()?)可以在格式化之前轉換值: ~~~ >>> import math >>> print('The value of PI is approximately {}.'.format(math.pi)) The value of PI is approximately 3.14159265359. >>> print('The value of PI is approximately {!r}.'.format(math.pi)) The value of PI is approximately 3.141592653589793. ~~~ 字段名后允許可選的?':'?和格式指令。這允許對值的格式化加以更深入的控制。下例將 Pi 轉為三位精度。 ~~~ >>> import math >>> print('The value of PI is approximately {0:.3f}.'.format(math.pi)) The value of PI is approximately 3.142. ~~~ 在字段后的?':'?后面加一個整數會限定該字段的最小寬度,這在美化表格時很有用: ~~~ >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678} >>> for name, phone in table.items(): ... print('{0:10} ==> {1:10d}'.format(name, phone)) ... Jack ==> 4098 Dcab ==> 7678 Sjoerd ==> 4127 ~~~ 如果你有個實在是很長的格式化字符串,不想分割它。如果你可以用命名來引用被格式化的變量而不是位置就好了。有個簡單的方法,可以傳入一個字典,用中括號訪問它的鍵: ~~~ >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} >>> print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; ' 'Dcab: {0[Dcab]:d}'.format(table)) Jack: 4098; Sjoerd: 4127; Dcab: 8637678 ~~~ 也可以用 ‘**’ 標志將這個字典以關鍵字參數的方式傳入: ~~~ >>> table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678} >>> print('Jack: {Jack:d}; Sjoerd: {Sjoerd:d}; Dcab: {Dcab:d}'.format(**table)) Jack: 4098; Sjoerd: 4127; Dcab: 8637678 ~~~ 這種方式與新的內置函數?vars()?組合使用非常有效。該函數返回包含所有局部變量的字典。 要進一步了解字符串格式化方法?str.format(),參見?_formatstrings_。 ### 7.1.1\. 舊式的字符串格式化 操作符?%?也可以用于字符串格式化。它以類似?sprintf()-style 的方式解析左參數,將右參數應用于此,得到格式化操作生成的字符串,例如: ~~~ >>> import math >>> print('The value of PI is approximately %5.3f.' % math.pi) The value of PI is approximately 3.142. ~~~ 因為?str.format()?還很新,大量 Python 代碼還在使用 % 操作符。然而,因為舊式的格式化方法最終將從語言中去掉,應該盡量使用 str.format()。 進一步的信息可以參見 _string-formatting_?一節。
                  <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>

                              哎呀哎呀视频在线观看