About usWhy usInstructorsReviewsCostFAQContactBlogRegister for Webinar

Join Function in Python

Software developers deal with strings frequently. Given that, the more flexibility a language can provide for dealing with strings, the easier it will become to create and manipulate strings the way they need to as per our requirements. 

One method in Python that serves this need by helping us create and concatenate strings flexibly is the join method, which we’ll discuss in this article.

We’ll learn:

  • What Is the join() Function in Python?
  • Uses of join() in Python
  • Join Function Example — Code and Output
  • FAQs

What Is the join() Function in Python?

The string method join() is a built-in function in Python that provides flexibility in creating and concatenating strings in Python from an iterable.

  • Parameter: Iterables are objects we can iterate over and return them one at a time. The join() function takes an iterable containing string type elements, like some sequence or a collection of strings represented as a list, string, array, tuple, dictionary, etc., as its parameter. 
  • Is Called On: The join() function is called on a string separator, which represents the separator the elements in the provided iterable will be concatenated using. 
  • Returns: It returns a string that concatenates the elements in the iterable, which are separated by the provided string separator.
  • TypeError: The join() method takes iterable containing values that only belong to the string data type. In case any non-string value is present in the iterable, we’ll get a TypeError.

Syntax:

stringseparator.join(iterable)

Note that the join() method takes the whole iterable as an argument, i.e., the join method concatenates each element present in the iterable using the separator string. We can’t selectively choose elements to concatenate.

Uses of join() in Python

  • We can create strings from iterable objects using join().
  • It helps us concatenate all the elements in an iterable using any preferred separator.
  • We can use it to perform a lot of tasks like:
    1. Combining a list of words into a sentence
    2. Separating a set of steps for a task using an arrow sign
    3. Combining a list of characters into a word by using an empty string as the separator
  • For a long list, adding a symbol after every element as a separator would be time taking and wasteful, and join removes the need for us to manually do that.

Join Function Example

Let us now understand this through an example using the join function in Python3. Here, we’ve taken different types of iterables containing string data: sequences like a list, a tuple, etc., and collections like a dictionary and joined them using different types of separators.

Code

listEx = ['Interview', 'Kickstart']

setEx = {'4', '8', '3', '2'}


tupleEx = ('Congrats',' That is amazing',' I can not believe it','')

tupleEx2 = ('Congrats',' That is amazing',' I can not believe it')


dictionaryEx = {'One':1,'Two':2, 'Three':3}


separatorSpace =' '

separatorExclamation ='!'

separatorThen =' then '

separatorArrow = '-->'


print(separatorSpace.join(listEx))

print(separatorExclamation.join(tupleEx))

print(separatorExclamation.join(tupleEx2))

print(separatorThen.join(dictionaryEx))

print(separatorArrow.join(setEx))

Output

Interview Kickstart

Congrats! That is amazing! I can not believe it!

Congrats! That is amazing! I can not believe it

One then Three then Two

8-->4-->3-->2

A few things to note here:

  • The dictionary’s key needs to be a string, but the value stored inside the key can be of another type, and we won’t get a TypeError.
  • It is NOT that the addition of one separator occurs after adding each element. A separator concatenates two elements. 
  • And so, as in the case of the exclamation mark (!), the last element will not have a separator after it by default. We must add the separator separately at the end. We can also add an empty string as an additional element in the list that’s then concatenated using the separator at the end.

FAQs

Question 1: If a dictionary has integer type keys and string type values, can we concatenate the values using the join() function?

Yes, we need to just put values as the iterable to achieve this. Here’s how that can be done:

dictionaryEx = {1:'One',2:'Two', 3: 'Three'}

separatorThen =' then '

print(separatorThen.join(dictionaryEx.values()))


Output:

One then Two then Three

Question 2: If a dictionary has integer type keys and string type values, what happens if we attempt concatenating the keys using the join() function?

We can’t concatenate keys of integer type using the join function. If we try to do that, we’ll get a TypeError, as can be seen here:

dictionaryEx = {1:'One',2:'Two', 3: 'Three'}

separatorThen =' then '

print(separatorThen.join(dictionaryEx))

Output:

stderr

Traceback (most recent call last):

  File "./file_name.py", line 3, in <module>

TypeError: sequence item 0: expected str instance, int found

Recommended Reading

Are you a software engineer looking for coding interview questions to aid your interview prep? Check out these useful pages for software developers:

Are You Ready to Nail Your Next Coding Interview?

Whether you’re a Coding Engineer gunning for Software Developer or Software Engineer roles, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!

If you’re looking for guidance and help with getting started, sign up for our free webinar. As pioneers in the field of technical interview prep, we have trained thousands of Software Engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!

Sign up now!

————

Article contributed by Tanya Shrivastava

Attend our Free Webinar on How to Nail Your Next Technical Interview

Recommended Posts

All Posts