albertjorlando
albertjorlando

Reputation: 212

Is there any need to use the "+" operator to concatenate strings when printing to the screen. {Using Version 3.3.1}

Is there any difference between these two procedures? Or for that matter a reason to use the + operator to concatenate strings?

    print('Hello, World!' + \
          'Hello, World!' + \
          'Hello, World!')

   # -----------------------------

    print('Hello, World!' \
          'Hello, World!' \
          'Hello, World!')

Upvotes: 1

Views: 82

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1124060

Yes, because the automatic concatenation of strings only applies to string literals.

It won't work with variable names:

print(string1
      string2
      string3)

is a syntax error.

The automatic concatenation is a feature of the parser; if you define multiple string literals within an expression that are not separated, they get auto-joined into one when compiling, not when running the code.

See String literal concatenation in the lexical analysis documentation.

Note that you don't need to use \ continuation slashes within parethesis (such as a function call).

Upvotes: 6

Related Questions