9

I am new to Python and need help trying to understand two problems i am getting relating to concatenating strings. I am aware that strings can be added to concatenate each other using + symbol like so.

>>> 'a' + 'b'
'ab'

However, i just recently found out you do not even need to use the + symbol to concatenate strings (by accident/fiddling around), which leads to my first problem to understand - How/why is this possible!?

>>> print 'a' + 'b'
ab

Furthermore, I also understand that the '\n' string produces a 'newline'. But when used in conjunction with my first problem. I get the following.

>>> print '\n' 'a'*7

a
a
a
a
a
a
a

So my second problem arises - "Why do i get 7 new lines of the letter 'a'. In other words, shouldn't the repeater symbol, *, repeat the letter 'a' 7 times!? As follows.

>>> print 'a'*7
aaaaaaa

Please help me clarify what is going on.

Jeremy
  • 1
  • 77
  • 324
  • 346
Abraham
  • 91
  • 1
  • 1
  • 2

4 Answers4

17

When "a" "b" is turned into "ab", this ins't the same as concatenating the strings with +. When the Python source code is being read, adjacent strings are automatically joined for convenience.

This isn't a normal operation, which is why it isn't following the order of operations you expect for + and *.

print '\n' 'a'*7

is actually interpreted the same as

print '\na'*7

and not as

print '\n' + 'a'*7
Jeremy
  • 1
  • 77
  • 324
  • 346
2

Python concatenates strings together when you do not separate them with a comma:

>>> print 'a' 'b'
ab

>>> print 'a', 'b'
a b

So you are actually printing '\na' 7 times.

zeekay
  • 46,885
  • 12
  • 100
  • 104
1
  1. I'm not sure what you mean by "how is it possible". You write a rule: two strings next to each other get concatenated. Then you implement it in the parser. Why? Because it allows you do conveniently do things like this:

    re.findall('(<?=(foo))'   # The first part of a complicated regexp
               '>asdas s'     # The next part
               '[^asd]'       # The last part
              )
    

    That way, you can describe just what you're doing.

  2. When you do A * B + C, the computer always does A times B first, then adds C, because multiplication comes before addition.

    When you do string concatenation by putting the string literals next to each other, and multiplication, the special string concatenation comes first. This means '\n' 'a' * 7 is the same as ('\n' 'a') * 7, so the string you're repeating is '\na'.

agf
  • 148,965
  • 36
  • 267
  • 227
1

You've probably already realised that relying on the implicit concatenation of adjacent strings is sometimes problematic. Also, concatenating with the + operator is not efficient. It's not noticeable if joining only a few small strings, but it is very noticeable at scale.

Be explicit about it; use ''.join()

print '\n'.join(['a'*7])
Rob Cowie
  • 21,066
  • 6
  • 59
  • 56
  • It's quite slow to do in a loop on many short strings because it has to get the '\n'.join() method every time, always try and keep it outside of the loop, or use map(str.join, sequence) instead. – agf Jul 23 '11 at 12:10
  • Er yeah, there are optimisations that can be made depending on context. OP didn't mention loops though. – Rob Cowie Jul 23 '11 at 18:31