python - 如何在 python 中的 %s 之后使用 %?

标签 python string

<分区>

我的问题归结为:

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

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

ofc 我可以只使用 format()

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

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

最佳答案

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

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

关于python - 如何在 python 中的 %s 之后使用 %?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29254854/

相关文章:

c++ - boost 字符串拆分以消除单词中的空格

ruby - 用 Ruby 将字符串中的元音大写

python - 使用 Peewee 选择一列

python - 带有 if 语句的 SQL INSERT

python - 在 Python 中使用 requests.Session() 作为上下文管理器来处理可变数量的连续请求

为字符串 "008949679851"抛出 java.lang.NumberFormatException

java - startsWith String 方法似乎无法正常工作

Python最近邻——坐标

python - 返回参数在 Python 中抛出异常

c - 查找字符串中某个字符的第一个索引