When you’re working with lists in Python, you may decide that you want to merge the contents of a list into a string. For instance, you may want to merge a list of all employee names in a business into one single string.
That’s where the Python string join()
method comes in. The join()
method allows you to take an existing list and convert it into a single string.
This tutorial will discuss, with examples, the basics of strings and how to use the Python join()
method to convert lists into a single string.
Python String: A Refresher
A string is a sequence of one or multiple characters. Strings are an essential data type in programming because they allow you to work with text data.
In Python, strings are declared within either single (‘’
) or double (“”
) quotes. Here’s an example of a string:
'This is a Python string, enclosed within single quotes.'
When you’re working with a list in Python, you may want to convert it into a string. That’s where the join()
method can be useful.
Python String join()
The join()
string method allows you to merge together all the elements in a list and convert the result into a string. The join()
method returns a string concatenated with the elements in a list.
The syntax for the join string formatting method is as follows:
separator.join(list_name)
separator
refers to the character(s) that should appear between each item in the newly-merged string, and list_name
is the name of the list whose values you want to merge into a single string.
Python String join() Example
Let’s walk through an example to discuss how the built-in function join()
works.
Suppose we have a list that contains the names of all workers in a company’s Sales Department. We want to concatenate the list into a string, whose values are separated with a comma. We could do so using this code:
employees = ['Donna Hanson', 'Anne Scott', 'Michael Parsons', 'Holly Harris'] separator = ',' new_employees = separator.join(employees) print(new_employees)
Our code returns:
Donna Hanson,Anne Scott,Michael Parsons,Holly Harris
Let’s break down our code, line-by-line. First, we declare a variable called “employees” which stores the names of all four employees in the Sales Department. Then, we declare a variable called separator
which stores the character we want to use to separate values in our newly-joined string later in our code.
Then, we use the join()
method to join together the contents of the “employees” list, separated using the value of the separator
variable (which, in this case, is a comma). Finally, we print the newly-joined string to the console.
You can see that, instead of being a list, our names are stored as a string. Each value in our list is separated using a comma.
Notice that the join()
method does not automatically add a space between each value in the new string. If we want a space to appear between the values in our new string, we would have to specify one in our separator
.
The join()
method also works with sets and tuples. Suppose we stored our employee names in a set (which is denoted using curly braces). We could convert the set into a string using the following same code as earlier, but instead of specifying a list, we can specify a set:
employees = {'Donna Hanson', 'Anne Scott', 'Michael Parsons', 'Holly Harris'} separator = ',' new_employees = separator.join(employees) print(new_employees)
Our code returns:
Donna Hanson,Anne Scott,Michael Parsons,Holly Harris
As you can see, the result of the join()
method is the same as it was from our above example. The difference in our code is that, instead of using a list (denoted by []
), we use a set (denoted by {}
). To use the join()
method with a tuple, we would use curly brackets (()
), which denote the set data type, instead of curly braces ({}
) as we did in this example.
Conclusion
The Python join()
method allows you to merge the contents of a list into a string, and add a separator between each value in the new string.
This tutorial discussed, with reference to examples, how to use the Python join()
method to join together the contents of a list. Now you’re ready to start using the join()
method like a Python professional!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.