Best way of using the Python Join method

  • Reading time:5 mins read

What is Python join method used for?

The python join method basically lets us take all the items in an iterable like a list and a tuple and join them together into one string. 

 

How can we use the Python join method?

We can also choose a separator that will separate each element in the string. Using a simple list of animals we are going to explore how the join method works.

 

python join

Using Python join method with string lists

After creating the list, we are going to also create a variable called ‘joined’ that will store the string that we have joined.

We are now going to equate the variable ‘joined’ to quotation marks within which we will specify the separator, which in this case is a comma.

Now in order to run the join method, we will use the dot notation and append the word join to the single quote marks followed by a pair of parentheses.

This is where we want to put the iterable the that we want to join together. So in our case, the iterable that we want to join together is a list.

 

 

When we run the above code we will get a new string with the elements from the iterable joined together and separated by a comma.

The join method is very important however the method only works on strings. If we change the separator from a comma into a dash we will get a string separated by a dash.

 

 

The join method can be used with any iterable, for instance, if we change the list of animals into a tuple we are still going to get the same response.

 

Using Python join method with dictionaries

The join method can also be applied to dictionaries as well. However, if we create a simple dictionary of animals with the name as the value and run the join method on the dictionary we will only get a string of keys joined together and not the values.

 

 

We can also get around this problem and join each key and value pair by looping through each one of them creating a tuple and then running the join method on the tuple.

 

python join

 

We are going to do a simple for loop through each key-value pair in the dictionary and this will give us tuples of each key-value pair from the dictionary.

 

 

Now since these are tuples we can run the join method on the tuples instead of just printing out the key itself. We can do that by simply specifying the separator and then running the join method on the key. As shown below.

 

 

So again the join method takes our operator and turns it into a joint list of strings with the separator in between each word. Again this only works with strings, so for instance, if we change the value of the key ‘elephant’ into an integer we run into a TypeError message.

 

 

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.