0
l='a'     
r='%sbb%%'%l    
print(r)

I expected the output abb%%, but the actual output is abb%.
Can someone explain why?

wim
  • 338,267
  • 99
  • 616
  • 750
  • 2
    Think about it. When `%` is used as a control character for substitutions in the string (_e.g._ `%s`), then you need a way to handle an _actual_ `%` character. A single one indicates that the next character should indicate what data type is being substituted. If that next character is a `%`, then it's interpreted as a `%` character with no substitution. – paddy Oct 16 '19 at 23:22
  • 1
    Possible duplicate of [How do I print a '%' sign using string formatting?](https://stackoverflow.com/questions/28343745/how-do-i-print-a-sign-using-string-formatting) – Ruzihm Nov 12 '19 at 02:35

1 Answers1

-1

The percent sign % is a special meta-character. I have described some examples below:

INPUTS:

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))

OUTPUTS:

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

NOTES:

+------+----------------------------------------------+
| %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               |
+------+----------------------------------------------+
Toothpick Anemone
  • 4,290
  • 2
  • 20
  • 42
  • 1
    No part of your answer addresses the case the question is actually asking about, which is `%%`. (Just adding a `%%` example isn't going to help, because the question already has a `%%` example. You're going to need to actually explain things.) – user2357112 Jan 20 '20 at 08:56