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

Lists and Concatenating Strings

Creating strings from a list of words is something you will do frequently when writing Python code. This lesson showed you how to create strings using str.join().

Python
words = ['cat', 'dog', 'horse', 'human']
print ' '.join(words)

This will print each word contained in words separated by a space.

00:00 The next idiom we’re going to discuss is lists and concatenating strings. As you can see here, we have a list of words = ['cat', 'dog', 'horse', 'human'].

00:12 Now when building strings, you want to add them together. Now this is the poor way of going about it. So you can take a list of words and pass it to the thing and iterate over those words, adding them to a longer_word. Now this will work.

00:31 As you can see, you have catdog—all concatenated in one together.

00:35 But the better way of going about this is using the .join(), which is a builtin in Python. Albeit this is a little bit confusing when you first look at it, but I assure you it’s the better and faster way of going about it. Right here, we have a string which is a space (' '), and then we have the .join() method, and then we have a bunch of words in our list. Now, if you were to run this, you’ll see that it produces each of the words with a space in between, and that’s what this portion of the statement will first do. For example, if you want it to be spaced by a comma, we’d simply change whatever that text is in there to a comma, and we’d have a comma there. We can put another animal in between each one, for example, we could put a 'frog' in between each one and do it that way, and that would produce catfrogdogfroghorsefrog and so on.

01:26 So that is the good way of going, also much faster and much more performant. We can also pass in lists as arguments as well, and that’ll produce my name on the screen as well. So if you ever end up concatenating a bunch of strings, try to shove them in a list and then use the .join().

Avatar image for gracebr28

gracebr28 on June 4, 2019

Shouldn’t the print_strings_good function be like this? def print_strings_good (strings): print (’ ‘.join(strings)) #use input instead

Instead of the list name, .join should pass the name of the function input (strings in this case) instead, so that when we call the function we can print any list that we want.

Avatar image for Abby Jones

Abby Jones on July 7, 2019

Was this done in 2.7? I think all we need to do in 3.6+ is add print() to make it work instead of print.

Avatar image for mikesult

mikesult on Feb. 24, 2020

I think both gracebr28 and Abby Jones are correct: The function should reference ‘strings’ inside of the function instead of ‘words’ (or change the parameter’s name to ‘words’). It works in this case because words is available to the function in this context. But if ‘words’ was named ‘words1’ or something else it doesn’t work. But the larger point about using join() instead of a loop with += concatenation is the important point that I learned as I’m a guilty party to using the += technique.

Thanks for the ’ ‘.join() tip.

Become a Member to join the conversation.