Loading video player…

Using the Plus Operator

00:00 You can use the + operator to concatenate strings in two different ways. By itself, when used to concatenate strings, + is known as, you guessed it, the concatenation operator.

00:10 This operator takes two string operands and concatenates them, creating a single combined string. Now, if you add an = sign, += is known as the augmented concatenation operator.

00:21 This operator is used to append a string to an existing string. But it’s important to note that string operations, like the augmented concatenation operator, are not performing true mutations on strings because strings are immutable objects.

00:35 Instead, they create new string objects linked to the original variable name. Now let’s go to the REPL for some examples.

00:45 First, let’s see an example of the concatenation operator applied to string literals. Write the following line of code: the string, “Hey” comma space, followed by the + operator, followed by the string “Pythonista!”

00:59 The result is the two strings combined into one, Hey, Pythonista! You can concatenate variables that hold strings in the same fashion. First, create the variable head. In it, store the string “String Concatenation “ with an extra space at the end.

01:16 Next, create the variable tail. In it, store the string “is Fun in Python!”

01:24 Now you can check the result: head + tail.

01:28 The output is the complete string, String Concatenation is Fun in Python! Note the extra space at the end of the first string. The + operator isn’t going to add spaces for you, so you need to remember that yourself.

01:41 When using variables, you may want to use the augmented concatenation operator to repeatedly extend a string. To see this, start by creating the variable word, and store within it the letters “Py”.

01:54 Now let’s use the augmented concatenation operator to extend this into a full word. word += the string “tho”, word += the string “nis”, word += the string “ta”, now view the contents of word.

02:14 It’s the complete string, “Pythonista”.

02:17 What’s important to remember though, is that both operators can only concatenate strings with other strings. If, for example, you try to concatenate an integer with a string, like combining the string, “The answer is:” with the integer 42, you’ll encounter a TypeError explaining what went wrong: can only concatenate str (not "int") to str.

02:39 To get around this, you’ll need to cast the non-string operand to a string using the built-in str() function. Input the following line of code: the string “The answer is: ” plus calling the str() function and passing in the integer 42.

02:55 The result is the concatenated string, The answer is: 42.

03:00 What you’ve used here are pretty much the most basic forms of string concatenation, and they’re super useful when you’re only dealing with a few strings. However, because Python strings are immutable objects, they cannot truly be modified in place.

03:13 Even with augmented concatenation, Python must create a new string object for every concatenation operation. This leads to extra overhead in terms of both memory and computation, and this only gets worse the more strings you wish to combine.

03:29 So what’s the alternative? That’s exactly what the next lesson is about, where we introduce the .join() method of the string class.

Become a Member to join the conversation.