In python, the string join method, join()
, is used to combine all the items of an iterable (example – list, tuple, set, etc.) into a single string separated by a separator.
ls = ["I", "like", "coding", "in", "python"] print(" ".join(ls))
Output:
I like coding in python
Table of Contents
- Syntax
- Examples
Syntax
The following syntax is used to apply the string join function in python.
string.join(iterable)
Note: The string
in the above syntax refers to the string separator to use for joining the items in the iterable.
Parameters:
The join()
function takes an iterable as its parameter. Iterable are objects capable of returning its member one at a time. List, tuple, set, dictionary, string, etc are all iterable objects.
Returns:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
A string resulting from the concatenation of the elements of the iterator and separated by the separator string passed.
Note: A TypeError
exception is raised if the iterator contains any non-string values.
Examples:
Example 1: Joining elements of a list
ls = ['a', 'b', 'c', 'd'] print("The iterable to join:", ls) # Using a single space as a separator print("Join using single space:", " ".join(ls)) # Using an empty string as a separator print("Join using empty string:", "".join(ls))
Output:
The iterable to join: ['a', 'b', 'c', 'd'] Join using single space: a b c d Join using empty string: abcd
In the above example, we see that the elements of the list are joined using the separator string provided.
Example 2: When the iterable contains a non-string value
ls = ['a', 1, 'b', 'c', 'd'] # Using a single space as a separator print("Join using single space:", " ".join(ls))
Output:
TypeError Traceback (most recent call last) <ipython-input-6-5f89fa28c958> in <module> 1 ls = ['a', 1, 'b', 'c', 'd'] 2 # Using a single space as a separator ----> 3 print("Join using single space:", " ".join(ls)) TypeError: sequence item 1: expected str instance, int found
In the above example, a TypeError
is raised as the iterable contained a non-string value.
Example 3: Joining elements of a tuple
tup = ('a', 'b', 'c', 'd') print("The iterable to join:", tup) # Using a single space as a separator print("Join using single space:", " ".join(tup)) # Using an empty string as a separator print("Join using underscore:", "_".join(tup))
Output:
The iterable to join: ('a', 'b', 'c', 'd') Join using single space: a b c d Join using underscore: a_b_c_d
Tuples are also iterable and hence the join function is able to concatenate its element into a single string using the separator provided.
Example 4: Joining elements of a set
set1 = {'a', 'b', 'c', 'd'} # Using a single space as a separator print("Join using comma:", ",".join(set1))
Output:
Join using comma: a,b,d,c
In the above example, the elements of the set have been joined using the separator ,
but the order is not the same as in the set we initialized. This is because a set is an unordered collection so you may get different sequences in output when working with sets.
Example 5: Joining elements of a dictionary
sample_dict = {'a': 1, 'b': 2, 'c': 3, 'd': 4} print(", ".join(sample_dict))
Output:
a, b, c, d
In the above example, we see that when joining a dictionary, its keys are joined together and not the values. If a dictionary has a non-string key, a TypeError
exception will be raised if you try to join it using the string join()
function.
For more on the join function refer to the python docs.
Other articles on python strings:
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.