TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
Python / Software Development

What Is the Python join() Function and When Should You Use It?

This tutorial shows you how to use the Python join() function, which takes all items in an iterable data structure and joins them into one string.
Aug 12th, 2024 5:00pm by
Featued image for: What Is the Python join() Function and When Should You Use It?
Featured image via Unsplash.

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:


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:


Simple. Now, let’s define it as my_list like so:


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:


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 from my_list.

We add a print statement, like so:


The entire code block looks like this:


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:


We’ll then use our variable to create a list like this:


We can now define another variable, applying the join() function like so:


Print it out with:


The entire code block looks like this:


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:


We then build our list from the input like so:


Join the elements with:


Print out the list with:


The entire code block looks like this:


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:


When using this method, your new join statement would look something like this:


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:


Let’s use the join function and separate each character with a space. That line of code would look like this:


Print it out with:


The entire code block looks like this:


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:


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:


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.

Group Created with Sketch.
TNS owner Insight Partners is an investor in: Simply.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.