What Is the Python join() Function and When Should You Use It?
Python contains several methods for joining elements together. You could cobble something together by way of the print()
function, you can use the sum()
function for numbers, or you could get really creative and reinvent that particular wheel.
Or, you could use the join()
function.
What is this function of which I speak?
Simply put, join()
joins every element in a sequence and returns a combined string. This is far more efficient (and more easily read) than using the plus operator and is more useful than the print()
function.
The join()
function can concatenate elements of an iterable data structure into a single string. The syntax of the join()
function looks like this:
1 |
separator.join(iterable) |
The separator is the string used to concatenate the elements and the iterable is the sequence of elements to be joined together. One of the handy things about join()
is that you get to define the separator. You could use any character or string of characters as the separator, depending on how you want the end result to look or how you need it to function.
You can use join()
with defined variables or with iterables (such as lists and tuples).
Let’s find out how this handy function works. We’ll start simply.
Say you have a list iterable with the following stored values:
apple
banana
cherry
That list looks like:
1 |
['apple', 'banana', 'cherry'] |
Simple. Now, let’s define it as my_list
like so:
1 |
my_list = ['apple', 'banana', 'cherry'] |
If we want to join those elements together, we use the join()
function. If we want to separate those elements with a comma, the join line would look like this:
1 |
result = ', '.join(my_list) |
Let me explain the above.
result
: the name of our new variable', '
: this is the separator, which we’ve defined as a comma and a space..join(my_list)
: instructs Python to join the iterables frommy_list
.
We add a print statement, like so:
1 |
print(result) |
The entire code block looks like this:
1 2 3 |
my_list = ['apple', 'banana', 'cherry'] result = ', '.join(my_list) print(result) |
Run the above code and the output will be:
apple, banana, cherry
We could also define a group of variables and wind up with the same results. First, we’ll define our fruit variables like this:
1 2 3 |
fruit1 = apple fruit2 = banana fruit3 = cherry |
We’ll then use our variable to create a list like this:
1 |
my_list = (fruit1, fruit2, fruit3) |
We can now define another variable, applying the join()
function like so:
1 |
fruit = ', '.join(my_list) |
Print it out with:
1 |
print(fruit) |
The entire code block looks like this:
1 2 3 4 5 6 7 8 9 |
fruit1 = "apple" fruit2 = "banana" fruit3 = "cherry" my_list = (fruit1, fruit2, fruit3) fruit = ', '.join(my_list) print(fruit) |
The output? You guessed it:
apple, banana, cherry
User Input
Let’s do the same thing but we’ll allow a user to input the names of the fruit and then we’ll use the join()
function to concatenate them together.
First, we accept user input like this:
1 2 3 |
fruit1 = input("Type the name of a fruit: ") fruit2 = input("Type the name of another fruit: ") fruit3 = input("Type the name of yet another fruit: ") |
We then build our list from the input like so:
1 |
my_list = (fruit1, fruit2, fruit3) |
Join the elements with:
1 |
fruit = ', '.join(my_list) |
Print out the list with:
1 |
print(fruit) |
The entire code block looks like this:
1 2 3 4 5 6 7 8 9 |
fruit1 = input("Type the name of a fruit: ") fruit2 = input("Type the name of another fruit: ") fruit3 = input("Type the name of yet another fruit: ") my_list = (fruit1, fruit2, fruit3) fruit = ', '.join(my_list) print(fruit) |
Run the code and the user will be asked (three times) to type the name of a fruit and the code will then create the list from the input.
Defining Your Separator
Another interesting route we can take is with the separator. You could define the separator as a variable like this:
1 |
s = ", " |
When using this method, your new join statement would look something like this:
1 |
s.join(my_list) |
You’ll get the same output as before.
Another fun little trick you could do is take a single string and break it up with a separator.
Let’s take the following variable:
1 |
name = "Jack Wallen" |
Let’s use the join function and separate each character with a space. That line of code would look like this:
1 |
str = ' '.join(name) |
Print it out with:
1 |
print(str) |
The entire code block looks like this:
1 2 3 4 5 |
name = "Jack Wallen" str = ' '.join(name) print(str) |
Run the above code and the output will be:
J a c k W a l l e n
You could get crafty and also define your separator like this:
1 2 3 4 5 6 |
name = "Jack Wallen" s = " " str = s.join(name) print(str) |
The output would be the same as above:
J a c k W a l l e n
You could also join a string with only two lines of code like this:
1 2 |
list1 = " new stack " print(" ".join(list1)) |
The output? You guessed it…
n e w s t a c k
And that, my friends, is how you use the join()
function to keep your code cleaner and more versatile.