Python

Python join() method Tutorial

Hello in this tutorial, we will understand the join() method in python programming.

1. Introduction

The join() method in python programming joins different elements and return a combined string. This method takes the iterable as an argument. This method returns the TypeError exception if the iterable contains any non-string values. It is represented by the following syntax –

Method syntax

string.join(iterable)

where,

  • string: Represents the parameter in which the joined elements of the iterable will be stored

Let us see the different use cases of the join() method in action.

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Python join() method Tutorial

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of your choice.

2.1 Python join() method Tutorial

Let us understand the different use cases of the join() method in python programming.

join() method

# join into a new string
delimiter = ','
programming_lang = ['java', 'python', 'typescript', 'javascript', 'html/css']
joined_str = delimiter.join(programming_lang)
print(joined_str)
# concatenation of strings
# using join() with an empty string to concatenate all strings in the iterable
audi = 'audi'
bmw = 'bmw'
mercedes = 'mercedes'
sequence = (audi, bmw, mercedes)
cars = ' '.join(sequence)
print(cars)
# using join() with a single string
welcome_str = 'greetings'
print('string characters are = {}'.format(','.join(welcome_str)))
# join() with set
# elements in the set are unordered and cannot be referred to by index or key
numbers = {'5', '1', '10', '2', '4', '3'}
print(" ".join(numbers))
# exception with join()
class Random:
    pass
r1 = Random()
r2 = Random()
randoms = [r1, r2]
# exception message - expected str instance, Random found
print(",".join(randoms))

If everything goes well the following output will be shown in the IDE console.

Console Output

== # join into new string ==
java,python,typescript,javascript,html/css
== # concatenation of strings ==
audi bmw mercedes
== # using join() with single string ==
string characters are = g,r,e,e,t,i,n,g,s
== # join() with set ==
3 5 1 10 2 4
== # exception with join() ==
Traceback (most recent call last):
  File "jcg-assignment-python-join.py", line 41, in 
    print(",".join(randoms))
TypeError: sequence item 0: expected str instance, Random found

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned:

  • join() method in python programming
  • Sample program to understand the join() method use cases

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was a tutorial on join() method in python programming.

Download
You can download the full source code of this example here: Python join() method Tutorial

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button