python - 为什么后来 double %% 转换为 single %
问题描述
l='a'
r='%sbb%%'%l
print(r)
我期望输出abb%%
,但实际输出是abb%
。
有人可以解释为什么吗?
解决方案
百分号%
是一个特殊的元字符。我在下面描述了一些例子:
输入:
print("Hello %s %s. Your current balance is %.2f" % ("John", "Doe", 53.4423123123))
print("Hello, %s!" % "Bob")
print("%s is %d years old." % ("Sarah", 43))
print("Ian scored %.0f%s on the quiz." % (98.7337, "%"))
lyst = [1, 2, 3]
print("id(lyst) == %d" % id(lyst))
print("id(lyst) in hexadecimal format is %x" % id(lyst))
输出:
Hello John Doe. Your current balance is 53.44
Hello, Bob!
Sarah is 43 years old.
Ian scored 99% on the quiz.
id(lyst) == 58322152
id(lyst) in hexadecimal format is 379ece8
笔记:
+------+----------------------------------------------+
| %s | String |
| %d | Integer |
| %f | Floating point number |
| %.2f | float with 2 digits to the right of the dot. |
| %.4f | float with 4 digits to the right of the dot. |
| %x | Integers in hex representation |
+------+----------------------------------------------+
推荐阅读
- javascript - 使用 D3 绘制趋势线 - 实际和预测
- r - 通过绑定元素从两个列表到一个列表
- java - 是否可以在 Java 中加载 TensorFlow 2.0 模型?
- scala - java.lang.ClassCastException: org.apache.spark.sql.catalyst.expressions.GenericRowWithSchema 不能被强制转换
- python - 'AttributeError:模块'__main__'没有属性'__package__'
- quartz.net - 每 30 分钟重复一次 Quarz.Net 作业……只在第一次工作
- node.js - 部分找不到车把
- javascript - 我试图在页面加载时生成一个变量,然后我想多次猜测,如何阻止变量在每次猜测时刷新?
- sql - 如何在标量函数中使用 SELECT CASE?
- excel - 当我将工作表复制并粘贴到新工作簿中时,如何确保复制所有条件格式?