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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Lists and Concatenating Strings

Give Feedback

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().

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

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

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.

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.

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.