8

In python 2.6, why is the following line valid?

my_line = 'foo' 'bar'

and if that is valid, why isn't the following:

my_list = 1 2 

The first example is string concatenation, however, the following isn't valid either (thanks god):

foo = 'foo'
bar = 'bar'
foo_bar = foo bar
4
  • 1
    I don't see the point in "removing" white spaces as your my_list= 1 2 example seems to suggest. Commented Dec 21, 2009 at 15:28
  • It doesn't suggest that. What I suggest is that this aspect of Python syntax is inconsistent (imho). Commented Dec 21, 2009 at 15:32
  • 3
    See this PEP python.org/dev/peps/pep-3126 where it is discussed extensively. Commented Dec 21, 2009 at 15:41
  • 2
    In any language, I suspect that 5 * 10 is valid but "foo" * "bar" is not. Does that make the language inconsistent? Even if so, what does that matter? Commented Dec 21, 2009 at 16:07

4 Answers 4

20

This is doing string literal concatenation. As noted in the documentation, advantages include the following:

This feature can be used to reduce the number of backslashes needed, to split long strings conveniently across long lines, or even to add comments to parts of strings...

It goes on to note that this concatenation is done at compilation time rather than run time.

The history and rationale behind this, and a rejected suggestion to remove the feature, is described in PEP 3126.

Sign up to request clarification or add additional context in comments.

Comments

8

my_line = 'foo' 'bar' is string concatenation.

9 Comments

@Ipthnc me too. Luckily I haven't come across code written this way, and I hope I never have to... I'm sure I'll forget what the heck it means!
C and C++ do this too. I don't know of any really good reason for Python to have the same quirk. In this particular case, 'foo' + 'bar' compiles to exactly the same bytecode, but IIRC the bytecode compiler does not always constant-fold string addition.
@Jason, I believe that's false, as this is done at compilation time, not run time. See my answers for a link that shows good reasons for having this feature.
It's so you can break up strings across multiple lines. You can do something like my_str = ('A really long string that's hard to fit on a line ' \n 'combined with another really long string on another line.') (Can't do proper formatting in comments, but imagine that spans two lines.) It's actually pretty useful and fairly common in Python source code. For what it's worth, C/C++ allow the same sort of automatic string concatenation.
@jldupont, one down-side of triple-quoted strings is that they include whitespace from any indentation. That would often force you to left-align everything in them, which can often break up the "flow" of code for the reader. I use triple-quoted strings for multiline constants, defined outside of classes or functions, but inside either I usually prefer the string concatentation approach.
|
6

Perhaps this is of C's ancestry. In C, the following is perfectly valid:

char* ptr = "hello " "world";

It is implemented by the C pre-processor (cpp), and the rationale given in that link is:

this allows long strings to be split over multiple lines, and also allows string literals resulting from C preprocessor defines and macros to be appended to strings at compile time

Comments

5

It isn't inconsistent. Strings and integers have different methods.

Integer concatenation is meaningless.

String concatenation is a meaningful default behavior.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.