Any language involves merging or combining strings. This process is referred to as concatenation.
The best way to describe it is when you take two separate strings – stored by the interpreter – and merge them so that they become one.
For instance, one string would be “hello” and the other would be “world.” When you use concatenation to combine them it becomes one string, or “hello world”.
There are different ways to do that, and we will discuss the most common methods. After, we will explore formatting, and how it works.
Concatenation:
In Python, there are a few ways to concatenate – or combine – strings. The new string that is created is referred to as a string object. Obviously, this is because everything in Python is an object – which is why Python is an objected-oriented language.
In order to merge two strings into a single object, you may use the “+” operator. When writing code, that would look like this:
str1 = “Hello”
str2 = “World”
str1 + str2
The final line in this code is the concatenation, and when the interpreter executes it a new string will be created.
One thing to note is that Python cannot concatenate a string and integer. These are considered two separate types of objects. So, if you want to merge the two, you will need to convert the integer to a string.