Loading video player…

Applying Concatenation Efficiently With .join()

00:00 Applying Concatenation Efficiently With the .join() method. The .join() method is a string method, meaning it must be called off of an existing string.

00:08 It concatenates multiple strings together using a separator building one new string object. Under the hood, this is done efficiently, without requiring the creation of intermediate string objects during the concatenation process.

00:22 Remember, strings are immutable, so this is a huge benefit when compared to the overhead encountered using multiple + or += operators for the same task.

00:31 Now as input, the .join() method takes an iterable of strings. As long as the contents are all strings, this can be any type that supports iteration, such as lists or tuples.

00:42 Because the .join() method is called on an existing string, that then becomes the separator of the concatenated output. Now, I invite you to join me in the REPL.

00:54 First, you’ll need some strings to work with, so create a list of strings and call it strings.

01:01 strings is a list of strings, each one a single word: "Hello", "world", "I", "am", "a", "Python", "ista".

01:08 To use the .join() operator, now you need an existing string. This can be a string literal. So start by creating a literal string with a single

01:18 space quote and call. join() passing in the list you created before, strings.

01:24 ".join(strings). The output, a single combined string: Hello, World! I am a Pythonista! By joining on a single space, each string in the original list is joined together separated by a space.

01:39 Not only is this efficient, it’s also super clean and readable. However, just like with the concatenation operator, you can still only join strings together with this method.

01:49 To test this, create a list of integers from one to five and call it numbers. And for the separator this time, let’s use a semicolon and a space, "; ".join(numbers) and the result is a TypeError: sequence item 0: expected string instance, int found.

02:14 What does this mean? From the very first element encountered by the .join() method, we get an error because the .join() method is only designed to work on strings.

02:23 The way we can fix this is again by casting each number to a string before passing them to the .join() method. "; ".join( and then pass a generator expression to the .join() method.

02:35 If you’re new to generator expressions, don’t worry, I’ll walk you through the code. They’re a useful technique to have in your toolkit, and this is a great use case.

02:43 And if you’ve ever used list comprehensions before, the syntax will also be very familiar.

02:49 str(number) for number in numbers

02:55 the output is our original list of numbers as one string each separated by a semicolon and a space. And while you could use a list comprehension here, a generator expression is more efficient because the numbers cast to string are fed to the .join() method one at a time. At a large scale, this would also make a huge difference in the performance of your program.

03:17 Next, let’s go back to discussing operators and see how you can apply the star operator to strings.

Become a Member to join the conversation.