String concatenation
String concatenation is the process of merging two or more strings to create a new string. In Python, there are several ways to concatenate strings. One of the most common ways is to use the +
operator. Now I imagine you saying "Wait a minute, didn't we use the +
operator to perform arithmetic operations?". You are right in saying that. However, the functionalities that the +
operator offers depend on which data type we are working with. We have already seen how this operator adds two integer or float values, but how can we use it with strings?
If we use the +
operators with strings, it will simply join them together. It will attach the first character of the string on the right to the last character of the string on the left. It sounds more difficult than it actually is. Take a look at the example below.
name = "James"
greeting = "Hello, I am " + name
print(greeting) # Output => Hello, I am James
Notice how I typed in a space after the word am
, because the concatenation directly joins the two strings, without separating them with a space.
As I have written previously, the functionalities of the +
operator vary based on which data type we are working with. If we are working with strings, it will simply join them, however, if we are working with other data types, such as integers, it will add them together. To better explain this, carefully look at the code below.
a = "5"
b = "3"
c = a + b
print(c) # Output => 53
d = 5
e = 3
f = d + e
print(f) # Output => 8
f-strings
In Python, f-strings, short for format strings, is a way to format strings and offers several benefits over other formatting and concatenating methods. When we saw the +
operator for concatenating strings, we did not reflect on how our code could end up being messy. If we want to concatenate more variables that are holding a string, we would have to add spaces every time we need a new word. To give you an example of this, look at the example below:
name = "John"
surname = "Smith"
country = "USA"
print("I am " + name + " " + surname + " and I live in the " + country)
# Output => Hello, my name is John Smith and I live in the USA
For this purpose were created f-strings. These particular type of formatting strings, allows you to keep your code clean and simple by improving its readability as well. The syntax for f-strings might seem a bit cryptic at first: before the quotes that signal the beginning of a string, we type the letter f
. Inside the quotes, whenever we want to add a value that is stored in a variable, we surround the variable name with {}
.
name = "John"
age = 24
print(f"My name is {name} and I am {age} years old")
# Output => My name is John and I am 24 years old
As you can see, the expressions within the curly braces are evaluated at runtime, making the resulting string very concise and easy to read. In addition, f-strings automatically convert values into strings, so you do not need to use typecasting. Pretty cool, right?
Conclusion
In this article, we took a look at two of the most common ways of operating with strings. We have seen how the +
operator joins strings together, but also how it can make our code difficult to read. On the other side, f-strings provide a concise and powerful way for formatting strings in Python and have quickly become a popular choice for developers.