l='a'
r='%sbb%%'%l
print(r)
I expected the output abb%%
, but the actual output is abb%
.
Can someone explain why?
l='a'
r='%sbb%%'%l
print(r)
I expected the output abb%%
, but the actual output is abb%
.
Can someone explain why?
The percent sign %
is a special meta-character. I have described some examples below:
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 |
+------+----------------------------------------------+