# 4.6 Concatenation

> `+` between texts glues them one after the other: `"Hola, " + "Ana"` is `"Hola, Ana"`.

Web page: https://pseudolearn.app/en/spec/expresiones/concatenacion

> **In one sentence**
>
> `+` between texts glues them one after the other: `"Hola, " + "Ana"` is `"Hola, Ana"`.

## 4.6.1 How it works

The same `+` sign that adds numbers **concatenates** texts. It accepts any combination of `String` and `Character`, and the result is always a `String`: even two single characters form a string of length 2.

**Figure 4.6-1: Concatenating three pieces**

Three boxes, "Hola, ", "Ana" and '!', with arrows converging on a larger box with "Hola, Ana!".

Each piece is copied after the previous one, from left to right, and forms a new string.

### Program: Gluing texts

```pseudolearn
Algorithm Saludo
  Define nombre As String <- "Ana";
  Write "Hola, " + nombre + '!';
  Write 'a' + 'b', " ", length('a' + 'b');
EndAlgorithm
```

**Output**

```text
Hola, Ana!
ab 2
```

*The program finishes normally.*

Concatenation **does not convert** numbers or booleans to text. To put a number in a message there are two ways: convert it with `ConvertirATexto`, or separate the pieces with commas in `Write`.

### Program: A number inside a text

```pseudolearn
Algorithm ConNumero
  Define edad As Integer <- 12;
  Write "Tienes " + toText(edad) + " años";
  Write "Tienes ", edad, " años";
EndAlgorithm
```

**Output**

```text
Tienes 12 años
Tienes 12 años
```

*The program finishes normally.*

> **Common mistakes**
>
> ### Program: Adding a text and a number
>
> ```pseudolearn
> Algorithm TextoMasNumero
>   Write "Edad: " + 5;
> EndAlgorithm
> ```
>
> **Diagnostics**
> - `cannotConcatenateNumberWithText` (error, 2:9): Cannot concatenate number with text; use conversion function or separate expressions with comma
>
> *The program finishes with an error.*

**In short**
- `+` between texts or characters concatenates them and gives a `String`.
- It does not convert numbers: use `ConvertirATexto` or commas in `Write`.
- It has the same precedence as addition.

**See also**
- [12.2 Text functions](https://pseudolearn.app/en/spec/biblioteca/texto.md)
- [12.3 Conversions](https://pseudolearn.app/en/spec/biblioteca/conversion.md)
- [4.7 Precedence and associativity](https://pseudolearn.app/en/spec/expresiones/precedencia.md)
