Interview Kickstart has enabled over 21000 engineers to uplevel.
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:
The string method join() is a built-in function in Python that provides flexibility in creating and concatenating strings in Python from an iterable.
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.
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.
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))
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:
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
Are you a software engineer looking for coding interview questions to aid your interview prep? Check out these useful pages for software developers:
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!
————
Article contributed by Tanya Shrivastava
Attend our webinar on
"How to nail your next tech interview" and learn