<bdo id='wGaEJ'></bdo><ul id='wGaEJ'></ul>
  • <tfoot id='wGaEJ'></tfoot>
        <legend id='wGaEJ'><style id='wGaEJ'><dir id='wGaEJ'><q id='wGaEJ'></q></dir></style></legend>

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

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

      1. Python3 - 在字符串格式化程序参数中使用变量

        Python3 - Use a variables inside string formatter arguments(Python3 - 在字符串格式化程序参数中使用变量)

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

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

                <tfoot id='U1Yr9'></tfoot>
                <legend id='U1Yr9'><style id='U1Yr9'><dir id='U1Yr9'><q id='U1Yr9'></q></dir></style></legend>
                  <bdo id='U1Yr9'></bdo><ul id='U1Yr9'></ul>
                  本文介绍了Python3 - 在字符串格式化程序参数中使用变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在打印一些格式化的列.我想使用以下变量来设置我的 .format 参数中的长度

                  I have some formatted columns that I'm printing. I would like to use the following variables to set the lengths in my .format arguments

                  number_length = 5
                  name_length = 24
                  viewers_length = 9
                  

                  我有

                  print('{0:<5}{1:<24}{2:<9}'.format(' #','channel','viewers'), end = '')
                  

                  理想情况下,我想要类似的东西

                  Ideally I would like something like

                  print('{0:<number_length}{1:<name_length}{2:<viewers_length}'.format(
                       ' #','channel','viewers'), end = '')
                  

                  但这给了我一个无效的字符串格式化错误.

                  But this gives me an invalid string formatter error.

                  我曾尝试在变量和括号前加上 %,但没有成功.

                  I have tried with % before the variables and parenthesis, but have had no luck.

                  推荐答案

                  你需要:

                  1. 也将名字用大括号括起来;和
                  2. 将宽度作为关键字参数传递给 str.format.

                  例如:

                  >>> print("{0:>{number_length}}".format(1, number_length=8))
                         1
                  

                  你也可以使用字典解包:

                  You can also use dictionary unpacking:

                  >>> widths = {'number_length': 8}
                  >>> print("{0:>{number_length}}".format(1, **widths))
                         1
                  

                  str.format 不会在本地范围内查找适当的名称;它们必须显式传递.

                  str.format won't look in the local scope for appropriate names; they must be passed explicitly.

                  对于您的示例,这可以像这样工作:

                  For your example, this could work like:

                  >>> widths = {'number_length': 5,
                                'name_length': 24,
                                'viewers_length': 9}
                  >>> template= '{0:<{number_length}}{1:<{name_length}}{2:<{viewers_length}}'
                  >>> print(template.format('#', 'channel', 'visitors', end='', **widths))
                  #    channel                 visitors
                  

                  (请注意,end 和任何其他显式关键字参数必须在 **widths 之前.)

                  (Note that end, and any other explicit keyword arguments, must come before **widths.)

                  这篇关于Python3 - 在字符串格式化程序参数中使用变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Kivy 1.9.0 Windows package KeyError: #39;rthooks#39;(Kivy 1.9.0 Windows 包 KeyError: rthooks)
                  Python Kivy: how to call a function on button click?(Python Kivy:如何在按钮单击时调用函数?)
                  How to disable a widget in Kivy?(如何禁用 Kivy 中的小部件?)
                  Centering an object in Kivy(在 Kivy 中将对象居中)
                  How to downgrade to Python 3.4 from 3.5(如何从 Python 3.5 降级到 Python 3.4)
                  Change button or label text color in kivy(在kivy中更改按钮或标签文本颜色)
                  <tfoot id='1uBmx'></tfoot>

                      <bdo id='1uBmx'></bdo><ul id='1uBmx'></ul>

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

                            <small id='1uBmx'></small><noframes id='1uBmx'>