String concatenation in Python is joining two or more strings into a single one. As a result, we create a new string that contains all the characters from the original string in the correct order. For example, if we have two strings, Hello
and world!
, concatenating them will produce the new string Hello world!
.
In Python, as in many other programming languages, there are different ways to concatenate strings. Let's look at all of them.
Let's take a classic example:
string1 = "Hello"
string2 = "world!"
string3 = string1 + ", " + string2
print(string3)
Hello, world!
Here we have given string1
the value Hello
and string2
the value world!
, then added them together by adding a comma with a space in the middle and printed the new string.
Here are a couple more examples of concatenation using the +
operator. Here is the concatenation of three strings:
string1 = "I"
string2 = "love"
string3 = "Python"
result = string1 + " " + string2 + " " + string3
print(result)
I love Python
As you can see, there are no differences. And this example:
first_name = "Monty"
last_name = "Python"
result = "My name is " + first_name + " " " + last_name
print(result)
My name is Monty Python
Note that in both examples, we have added spaces between words to get the correct output from a printing perspective. It is recommended to do this in other cases as well.
In the examples above, we used the +
operator to join two or more strings. But string concatenation in Python 3 can also be done using the join()
method. Here is our first example written in this way:
strings = ["Hello", "world!"]
string3 = ", ".join(strings)
print(string3)
In the output, we will see the same Hello, world!
.
Here are two other examples rewritten using the join()
method:
strings = ["I", "love", "Python"]
result = " ".join(strings)
print(result)
I love Python
first_name = "Monty"
last_name = "Python"
strings = ["My name is", first_name, last_name]
result = " ".join(strings)
print(result)
My name is Monty Python
In both examples, we created a list of strings and then used the join()
method to join them into a single string. In the first example, we only added a space to separate the words, while in the second example we also involved variables in the list.
-
If we try to concatenate a string and a number, we get an error:
age = 28
message = "I am " + age + " years old."
print(message)
Traceback (most recent call last):
File "C:/Python/Python38/concat1.py", line 2, in <module>
message = "I am " + age + " years old."
TypeError: can only concatenate str (not "int") to str
This happens when we use an incorrect syntax. In this case, the code will cause a TypeError
because we are trying to concatenate a string with a number without converting the number to a string as well.
To fix this error, we can use str()
to convert a number to a string. Here is an example of proper string and number concatenation:
age = 28
message = "I am " + str(age) + " years old."
print(message)
I am 28 years old.
We converted the number enclosed in the age
variable into a string using str()
, and the interpreter gave us the desired result.
The same thing can be done using the format()
method:
age = 28
message = "I am {} years old.".format(age)
print(message)
I am 28 years old.
This method works the same way as str()
, only the syntax differs.
Another way to get the same result is to use a new feature of version 3, f-string
:
age = 28
message = f"I am {age} years old."
print(message)
I am 28 years old.
Python's f-string
(formatted string literals) works like the str.format()
method but provides a more convenient and concise way to put values into a string. With f-string
, we can insert variable values directly inside a string by using curly braces {}
and specifying the variable name inside those braces. Here are some more examples of string and number concatenation using f-string
:
name = "Monty"
age = 35
print(f"My name is {name} and I am {age} years old.")
My name is Monty and I am 35 years old.
salary = 100000
tax_rate = 0.25
taxes = salary * tax_rate
print(f"My salary is {salary} and my taxes are {taxes}.")
My salary is 100000 and my taxes are 25000.0.
As you can see, we can insert variable values inside a string using curly braces, and Python automatically substitutes variable values in these places when the program is executed. Note that f-string also supports various output formats that can be specified inside curly braces. For example, you can specify the number of decimal places for floating point numbers. This makes f-string an even more powerful formatting tool.
With the concatenation, we have once again seen how flexible Python is, because you can join strings using a variety of methods. Even concatenating a string to a number by converting a numeric value to a string value can be done in at least three ways. Just choose your favorite.