String operations

Joining strings

Strings can be joined together with a string concatenation operation. This operation is denoted by the sign +, just like the operation of summation, so in programming concatenation is often informally called string addition.

Sometimes, we may have an integer or a real number written in a string, so it is important to understand that the sign + refers to addition when talking about numbers, and when strings are involved it refers to the concatenation of strings. For example, in the following program, the first a + b is the addition of numbers, and the second is the addition of strings. Accordingly, the printed results also differ (try it out).

It is likely that occasionally you may be confused by the result when executing a program. The result may be different than expected for many reasons, and one possibility is that you unintentionally added up strings instead of numbers.

The + character can stand between two numeric expressions or between two strings, but not between a string and a number (in any order). Such combinations result in a TypeError (try it).

String multiplication

Strings can also be multiplied. This means that it is allowed to multiply a string by an integer (either from left or right), and the result is a new string, which is obtained by repeating a given string a given number of times.

In the following example we underline the numbers with a line, and that line is obtained as a result of multiplying the string ‘-’ by 12.