【问题标题】:How to use a % after %s in python? [duplicate]如何在 python 中的 %s 之后使用 %? [复制]
【发布时间】:2015-03-25 19:56:29
【问题描述】:

我的问题归结为:

# examplecode
# i want to achieve the string "1.1%"

s = '%s%' % '1.1'
# but this throws me a
ValueError: incomplete format

我可以使用format()

s = '{}%'.format('1.1')

但是有没有办法在%s 之后使用%

【问题讨论】:

    标签: python string


    【解决方案1】:

    用另一个% 转义%,像这样

    >>> s = '%s%%' % '1.1'
    >>> s
    '1.1%'
    

    【讨论】:

      【解决方案2】:

      你可以把 % 翻倍。

      s = '%s%%' % '1.1'
      

      【讨论】:

        【解决方案3】:

        加倍 %,"%s%%" % 10 应该给你10%

        【讨论】:

          猜你喜欢
          • 2017-03-07
          • 2011-08-25
          • 2015-04-27
          • 2014-01-25
          • 2019-01-24
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多