0

I have searched stackoverflow and I can't find the answer that I am looking for. Apologies if this sounds like a dumb question since I am a newbie learning Python. Spent 1 hour trying to understand this and I can't grasp the concept.

Can somebody explain to me the following:

hilarious = False
joke_evaluation = "Isn't that joke so funny?! %r"

print joke_evaluation % hilarious 

hilarious = "False"
joke_evaluation = "Isn't that joke so funny?!"

print hilarious + joke_evaluation

Why is it that you can't combine the first with + but %.

Is it because in the second one, they are both defined strings with quotations but in the first, hilarious = False is not in quotations?

Keith Thompson
  • 254,901
  • 44
  • 429
  • 631
  • possible duplicate of [What does the percentage sign mean in Python 3.1](http://stackoverflow.com/questions/961344/what-does-the-percentage-sign-mean-in-python-3-1) – msw Nov 07 '14 at 02:14

2 Answers2

2

The % operator on strings isn't exactly a concatenation like the + operator is.

With % you're actually substituting placeholders in the string on the left side of % with values from the right side.

So you could have something like this:

"This is my %s string" % "fantastic"

would yield:

This is my fantastic string

See how you're not concatenating the strings but "inserting" into the string on the right side.

See the documentation for more details.

Update:

As pointed out in the comments below, there are two "issues" with this. As of Python 2.5, this is actually the "old" way of doing string substitution. These days the following format is preferred (kudos to asmacdo)

 "This is my {adjective} string".format(adjective='fantastic')

As well in the comments (thanks ErlVolton) I should explain that the "%s" refers to a string substitution. That is, the value that gets put in there should be a string. Similarly you can have integer substitution ("%d"), decimal floating point substitution ("%f") and, as in the case of the original question, you can substitute boolean values with "%r". You can also do a lot more formatting (vary the number of decimal places for a floating point number, pad numbers with leading zeros etc.) which is explained much better in the docs.

Finally, it's worth mentioning that you can substitute multiple values into a string but that changes the syntax a tiny bit. Instead of having a single value after the % operator you need to use a tuple. Example:

"this %s substitutes strings, booleans (%r) and an integer (%d)" % ('string', True, 42)

would yield:

this string substitutes strings, booleans (True) and an integer (42)
Community
  • 1
  • 1
StFS
  • 1,639
  • 2
  • 15
  • 31
  • 2
    This is correct, but it should be noted that this is not the preferred way to do string formatting unless you need to be able to work with 2.5. "This is my {adjective} string".format(adjective='fantastic') is preferred. – asmacdo Nov 07 '14 at 02:18
  • 1
    You should update your answer to explain that formatting also does implicit type conversion, `"foo " + 4` will fail where `"foo %d" % 4` does not. – ErlVolton Nov 07 '14 at 02:33
0

In this case, the percent sign marks the start of a printf-style specifier. When the first argument is a string, it formats it using the second argument (a boolean value in this case).

Refer to the documentation, or check out this question. They should shed some light on the situation.

As for the plus sign, you simply can't add (or concat) a boolean value to a string.

Community
  • 1
  • 1
Daniel Bidulock
  • 2,344
  • 1
  • 26
  • 27