<i id='zHDhz'><tr id='zHDhz'><dt id='zHDhz'><q id='zHDhz'><span id='zHDhz'><b id='zHDhz'><form id='zHDhz'><ins id='zHDhz'></ins><ul id='zHDhz'></ul><sub id='zHDhz'></sub></form><legend id='zHDhz'></legend><bdo id='zHDhz'><pre id='zHDhz'><center id='zHDhz'></center></pre></bdo></b><th id='zHDhz'></th></span></q></dt></tr></i><div id='zHDhz'><tfoot id='zHDhz'></tfoot><dl id='zHDhz'><fieldset id='zHDhz'></fieldset></dl></div>
    • <bdo id='zHDhz'></bdo><ul id='zHDhz'></ul>

  1. <small id='zHDhz'></small><noframes id='zHDhz'>

    1. <legend id='zHDhz'><style id='zHDhz'><dir id='zHDhz'><q id='zHDhz'></q></dir></style></legend>
    2. <tfoot id='zHDhz'></tfoot>

      无法弄清楚字符串替换在 Python 3.x 中是如何工作

      时间:2023-10-09

        • <bdo id='22YmE'></bdo><ul id='22YmE'></ul>
            <i id='22YmE'><tr id='22YmE'><dt id='22YmE'><q id='22YmE'><span id='22YmE'><b id='22YmE'><form id='22YmE'><ins id='22YmE'></ins><ul id='22YmE'></ul><sub id='22YmE'></sub></form><legend id='22YmE'></legend><bdo id='22YmE'><pre id='22YmE'><center id='22YmE'></center></pre></bdo></b><th id='22YmE'></th></span></q></dt></tr></i><div id='22YmE'><tfoot id='22YmE'></tfoot><dl id='22YmE'><fieldset id='22YmE'></fieldset></dl></div>
            <tfoot id='22YmE'></tfoot>

              <tbody id='22YmE'></tbody>

            1. <small id='22YmE'></small><noframes id='22YmE'>

              • <legend id='22YmE'><style id='22YmE'><dir id='22YmE'><q id='22YmE'></q></dir></style></legend>
                本文介绍了无法弄清楚字符串替换在 Python 3.x 中是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                在 python 2.x 中,你可以这样做:

                In python 2.x you were allowed to do something like this:

                >>> print '%.2f' % 315.15321531321
                315.15
                

                但是,我无法让它在 python 3.x 上工作,我尝试了不同的方法,例如

                However, i cannot get it to work for python 3.x, I tried different things, such as

                >>> print ('%.2f') % 315.15321531321
                %.2f
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                TypeError: unsupported operand type(s) for %: 'NoneType' and 'float'
                
                >>> print ("my number %") % 315.15321531321
                my number %
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                TypeError: unsupported operand type(s) for %: 'NoneType' and 'float'
                

                然后,我阅读了 .format() 方法,但我也无法让它工作

                Then, I read about the .format() method, but I cannot get it to work either

                >>> "my number {.2f}".format(315.15321531321)
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                AttributeError: 'float' object has no attribute '2f'
                
                >>> print ("my number {}").format(315.15321531321)
                my number {}
                Traceback (most recent call last):
                  File "<stdin>", line 1, in <module>
                AttributeError: 'NoneType' object has no attribute 'format'
                

                我很乐意收到任何提示和建议!

                I would be happy about any tips and suggestions!

                推荐答案

                尝试发送带有格式的整个字符串进行打印.

                Try sending the entire string with formatting to print.

                print ('%.2f' % 6.42340)
                

                适用于 Python 3.2

                Works with Python 3.2

                此外,该格式通过为所提供的 agruments 提供索引来工作

                In addition, the format works by providing an index to the provided agruments

                print( "hello{0:.3f}".format( 3.43234 ))
                

                注意格式标志前面的0".

                Notice the '0' in front of the format flags.

                这篇关于无法弄清楚字符串替换在 Python 3.x 中是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:格式化连续数字 下一篇:如何在 Python matplotlib 的 LaTeXed 下标中包含字符串

                相关文章

                最新文章

                <legend id='X0KlV'><style id='X0KlV'><dir id='X0KlV'><q id='X0KlV'></q></dir></style></legend>

                <small id='X0KlV'></small><noframes id='X0KlV'>

              • <i id='X0KlV'><tr id='X0KlV'><dt id='X0KlV'><q id='X0KlV'><span id='X0KlV'><b id='X0KlV'><form id='X0KlV'><ins id='X0KlV'></ins><ul id='X0KlV'></ul><sub id='X0KlV'></sub></form><legend id='X0KlV'></legend><bdo id='X0KlV'><pre id='X0KlV'><center id='X0KlV'></center></pre></bdo></b><th id='X0KlV'></th></span></q></dt></tr></i><div id='X0KlV'><tfoot id='X0KlV'></tfoot><dl id='X0KlV'><fieldset id='X0KlV'></fieldset></dl></div>

                  1. <tfoot id='X0KlV'></tfoot>

                      <bdo id='X0KlV'></bdo><ul id='X0KlV'></ul>