Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Concatenating & Joining Strings in Python

Concatenating refers to “gluing things together”. String concatenation is the opposite of splitting. In this lesson, you’ll see some of the ways to concatenate or join strings together. The simplest and most common way is to use the plus symbol to add multiple strings together:

Python
>>> 'a' + 'b' + 'c'
'abc'
You’ll also learn how to combine several strings from an iterable into a single string using the .join() method:

Python
>>> city = ['BOISE', 'IDAHO', 'US']

>>> ' '.join(city)
'BOISE IDAHO US'

00:01 Welcome to the third video in the series Splitting, Concatenating, and Joining Strings in Python. In the first two videos, we learned about strings and how to create multiple strings out of one string using the string method .split(). In this video, we will demonstrate taking multiple strings and combining them into one.

00:22 Let’s begin with some string literals separated by a plus sign (+). We’re going to use the plus sign, which is typically the symbol we most often associate with mathematical addition. In fact, you may have previously done something like this by accident. Here, we prompt the user to enter a Celsius temperature with the intent to calculate its Fahrenheit equivalent.

00:43 We forget to convert the user’s input from a string to numerical data, and Python warns us that it can’t understand using math on mismatched data types like this.

00:53 There is an exception to this role and it’s coming up later in the video, but for the most part, Python does not like dissimilar data types on either side of a math operator.

01:03 We could fix this error by converting our input value to an int or float.

01:09 Returning to our example of the plus sign between two strings, this expression will produce a new single string that is the combination of the two. This operation is known as concatenation.

01:22 We may use concatenation whenever we want to combine two or more strings into one string. That one string, of course, is a newly created string. And that’s because, as you may recall, strings are immutable, so concatenation cannot change either of the original strings.

01:39 Concatenation produces a new string, and if you want to retain that string, you will want to store it in a new variable.

01:50 As promised earlier, there is an occasion when Python allows for a math operator to work on dissimilar data types, and that is with the multiplication operator (*). Here, the effect of multiplying the string 'Python' by the integer value 5 is that we have five 'Python' strings combined into one resulting string value.

02:11 What if the strings you want to combine happen to be in a list? No problem! We can join them. .join() is a string method that requires an iterable as its argument.

02:23 Arguments that are lists, or even strings or tuples, are fair game. The iterable has to have strings as its element type, however. You can think of the .join() method as the opposite of the .split() method.

02:43 Since we know that .join() takes string elements of an iterable and combines them into one string, we know that it is also similar to concatenation.

02:53 But unlike concatenation, it’s a string method, which means we have to have some string to start out with in order to invoke this method. So, what string do we use? The correct answer to that question is, it’s up to you!

03:09 Let’s observe how .join() operates. I’m going to start with a simple string literal character, say lowercase 'x'.

03:18 I’m going to invoke the .join() method from 'x' and pass it the string 'Python'. Interesting result! Now let’s try passing a list of strings.

03:29 You can see the effects of invoking .join() on a string is that that string is now placed as a separator among strings in the sequence. What happens if we pass in just one character or a single item list?

03:44 It turns out our separator only separates if there is more than one item in the iterable sequence. In other words, it only serves to be between elements.

03:55 If we don’t want a separator at all, we can invoke .join() using an empty string.

04:02 We can also use .join() to sort of undo a list that’s been created by .split(). So, how might we use .join() in a practical way?

04:11 Let’s revisit our example from the last video. Often, database queries are returned to us as tuples. We may use the .join() method to create a CSV from a database query.

04:23 This may involve an extra step to convert certain field values to strings, but let’s look at a straightforward example.

04:31 Here are the results of a database query for U.S. zip codes in the state of Kentucky. I want to create a CSV from this data, perhaps to work with it in a spreadsheet program. No problem!

04:44 Since my data is already in string format, this is even easier. I can just loop through the tuples and join them with a comma. At the same time, I can add them to a new CSV file. And look!

04:56 I even made use of concatenation in the process.

05:03 Thank you for joining me for, well, string joining. I hope you found some new tricks and you can apply these in your future projects.

Become a Member to join the conversation.