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

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                ## 概述 python自2.6后,新增了一種格式化字符串函數`str.format()`,威力十足,可以替換掉原來的`%` 注:以下操作版本是python2.7 ### % 格式化字符串 ~~~ %[align=>][width=auto][.precision] ~~~ ### format格式 ![](https://ws4.sinaimg.cn/large/006tKfTcgy1frxs71hqt8j31ic0bqqh2.jpg) ~~~ :[[fill=' '][align=>]][width=auto][.precision][typecode=d] ~~~ ~~~ fill 填充 align <左對齊 >右對齊 =標記字符后的填充 ^居中 width 整數部分的寬度 .precision 小數部分的寬度,包括'.' typecode 進制 ~~~ ### 語法 通過{} 和 : 替換 % #### 通過位置 ~~~ >>> '{0} is {1}'.format('jihite', '4 years old') 'jihite is 4 years old' >>> '{0} is {1} {0}'.format('jihite', '4 years old') 'jihite is 4 years old jihite' ~~~ 通過format函數可以接受不限參數個數、不限順序 #### 通過關鍵字 ~~~ >>> '{name}:{age}'.format(age=4,name='jihite') 'jihite:4' >>> '{name}:{age}'.format(age=4,name='jihite',locate='Beijing') 'jihite:4' ~~~ format括號內用=給變量賦值 #### 通過對象屬性 ~~~ >>> class Person: ... def __init__(self, name, age): ... self.name,self.age = name, age ... def __func__(self): ... return "This guy is {self.name}, is {self.age} old".format(self=self) ... >>> s =Person('jihite', 4) >>> s.__func__() 'This guy is jihite, is 4 old' ~~~ #### 通過下標 其實就是通過位置 ~~~ >>> '{0[0]} is {0[1]} years old!'.format(['jihite', 4]) 'jihite is 4 years old!' >>> '{0} is {1} years old!'.format('jihite', 4) 'jihite is 4 years old!' ~~~ ### 格式限定符 通過{} : 符號 #### 填充和對齊 ^<>分別表示居中、左對齊、右對齊,后面帶寬度 ~~~ >>> '{:>10}'.format('jihite') ' jihite' >>> '{:<10}'.format('jihite') 'jihite ' >>> '{:^10}'.format('jihite') ' jihite ' ~~~ #### 精度和類型f 精度常和f一起使用 ~~~ >>> '{:.2f}'.format(3.1415) '3.14' >>> '{:.4f}'.format(3.1) '3.1000' ~~~ #### 進制轉化 ~~~ >>> '{:b}'.format(10) '1010' >>> '{:o}'.format(10) '12' >>> '{:d}'.format(10) '10' >>> '{:x}'.format(10) 'a' 其中b o d x分別表示二、八、十、十六進制 ~~~ #### 千位分隔符 ~~~ >>> '{:,}'.format(1000000) '1,000,000' >>> '{:,}'.format(100000.23433) '100,000.23433' >>> '{:,}'.format('abcedef') Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: Cannot specify ',' with 's'. ~~~ 這種情況只針對數字 #### 填充 此處只寫字符,不用加引號 ~~~ '{:h>15}'.format('hiyang') 'hhhhhhhhhhiyang' ~~~
                  <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>

                              哎呀哎呀视频在线观看