<small id='9FwLz'></small><noframes id='9FwLz'>

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

    • <bdo id='9FwLz'></bdo><ul id='9FwLz'></ul>
    1. <legend id='9FwLz'><style id='9FwLz'><dir id='9FwLz'><q id='9FwLz'></q></dir></style></legend>

    2. <tfoot id='9FwLz'></tfoot>

        python字符串格式调用函数

        时间:2023-10-08
          <tbody id='9XYAM'></tbody>
          <legend id='9XYAM'><style id='9XYAM'><dir id='9XYAM'><q id='9XYAM'></q></dir></style></legend>
            <i id='9XYAM'><tr id='9XYAM'><dt id='9XYAM'><q id='9XYAM'><span id='9XYAM'><b id='9XYAM'><form id='9XYAM'><ins id='9XYAM'></ins><ul id='9XYAM'></ul><sub id='9XYAM'></sub></form><legend id='9XYAM'></legend><bdo id='9XYAM'><pre id='9XYAM'><center id='9XYAM'></center></pre></bdo></b><th id='9XYAM'></th></span></q></dt></tr></i><div id='9XYAM'><tfoot id='9XYAM'></tfoot><dl id='9XYAM'><fieldset id='9XYAM'></fieldset></dl></div>

              • <tfoot id='9XYAM'></tfoot>
                  <bdo id='9XYAM'></bdo><ul id='9XYAM'></ul>

                • <small id='9XYAM'></small><noframes id='9XYAM'>

                  本文介绍了python字符串格式调用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有没有办法用新的格式语法格式化来自函数调用的字符串?例如:

                  Is there a way to format with the new format syntax a string from a function call? for example:

                  "my request url was {0.get_full_path()}".format(request)
                  

                  所以它调用函数get_full_path函数inside字符串而不是作为格式函数中的参数.

                  so it calls the function get_full_path function inside the string and not as a parameter in the format function.

                  这是另一个可能会更好地表达我的挫败感的例子,这就是我想要的:

                  Here is another example that will probably show my frustration better, this is what I would like:

                  "{0.full_name()} {0.full_last_name()} and my nick name is {0.full_nick_name()}".format(user)
                  

                  这是我想要避免的:

                  "{0} and {1} and my nick name is {2}".format(user.full_name(), user.full_last_name(), user.full_nick_name())
                  

                  推荐答案

                  Python 3.6 添加了文字字符串插值,它是用 f 前缀编写的.请参阅 PEP 0498 -- 文字字符串插值.

                  Python 3.6 adds literal string interpolation, which is written with an f prefix. See PEP 0498 -- Literal String Interpolation.

                  这样可以写

                  >>> x = 'hello'
                  >>> s = f'{x}'
                  >>> print(s)
                  hello
                  

                  应该注意,这些不是实际的字符串,而是表示每次都计算为字符串的代码.在上面的示例中,s 的类型为 str,值为 'hello'.你不能传递一个 f-string,因为它会在使用前被评估为结果 str(不像 str.format,但与所有其他字符串文字修饰符一样,例如 r'hello'b'hello''''hello''').(PEP 501 -- 通用字符串插值(目前推迟)建议使用字符串将评估为稍后可以进行替换的对象的文字.)

                  It should be noted that these are not actual strings, but represent code that evaluates to a string each time. In the above example, s will be of type str, with value 'hello'. You can't pass an f-string around, since it will be evaluated to the result str before being used (unlike str.format, but like every other string literal modifier, such as r'hello', b'hello', '''hello'''). (PEP 501 -- General purpose string interpolation (currently deferred) suggests a string literal that will evaluate to an object which can take substitutions later.)

                  这篇关于python字符串格式调用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:pandas xlsxwriter,格式头 下一篇:带有 SQL 通配符和 LIKE 的 Python 字符串格式

                  相关文章

                  最新文章

                  • <bdo id='LgG3H'></bdo><ul id='LgG3H'></ul>

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

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

                      <tfoot id='LgG3H'></tfoot>