Understanding Python String join()
Python String join()
In Python, String join() is an inbuilt function that is used to return the result when a string is concatenated with an iterable like list , tuple, dictionary, set, etc.
- With this
join(), function any string can be concatenated flexibly. - Basically with this function, every single element of an iterable (like list, tuple, dictionary, set, etc) gets concatenated with the string and the concatenated string is returned as a result.
- If there is a non-string value in an iterable then
TypeErrorexception will be raised. - Also Python String
join()method is the method that is used to return the string in which the elements of any sequence have been joined by using string separator.
Python String **join()**: Syntax
Below we have a basic syntax of String join() in Python:
string.join(iterable)Python String **join()**: parameters
iterable ( It is an object whose all values are returned in the form of a string); is a parameter of join() function of Python Strings. It has some values and these are given below:
- list
- string
- tuple
- dictionary
- set
Python String **join()**: Return Value
- Whenever
non-string valuesare there in an iterable thenTypeErrorexception will be raised. - Whenever a dictionary is used as an iterable, then returned values are the keys, not the values.
- Whenever
string valuesare there in an iterable concatenated string that is returned as a result.
Python String **join()** when Tuple is an iterable
Here we are going to define a tuple with its element and then after we will join by using .join() function:
fruits= ('Mango', 'Apple', "Orange")
str = '|'.join(fruits)
print(str)In the above example we have first defined a tuple and then join that tuple values with the | as you can see the output below:
Mango|Apple|Orange
Python String **join()** when List is used as an iterable
In the python script below, we have tried using the join() function with a list ( A data structure in Python is a mutable/changeable, ordered sequence of elements) as an iterable:
x = ['WonderWoman', 'Aquaman', 'Batman', 'Superman']
movies = '||'.join(x)
print(movies)In the above example we have first defined a list and then join that list values with the || as you can see the output below:
WonderWoman||Aquaman||Batman||Superman
Python String **join()** when Dictionary is used as an iterable
In the python script below, we have tried using the join() function with a dictionary ( Unordered collection of data values; It holds the key: value pair) as an iterable:
technologies = {
1: 'AI',
2: 'Machine Learning',
3: 'Serverless',
4: 'ARVR'
}
trends= '/'.join(technologies.values())
print(trends)In the above example, we have used dictionary values and join them with a separator /. The output for the same is given below:
AI/Machine Learning/Serverless/ARVR
Python String **join()** when Set is used as an iterable
In the python script below, we have tried using the join() function with a set ( a collection of elements which is unordered and unindexed) as an iterable:
a= {'11', '21', '29'}
bc = ', '
print(bc.join(a))In the above example, we have used a set and join them with a separator , .As set is unordered so you will see elements in a different order in the output. The output for the same is given below:
29, 11, 21
Using non-string values in an iterable
Whenever non-string values are there in an iterable then TypeError exception will be raised. Let us see this with the help of an example given below:
a=(11,21)
b='/'
print(b.join(a))Here we have the output for the same:
Traceback (most recent call last): File "<pyshell#18>", line 1, in <module> print(b.join(a)) TypeError: sequence item 0: expected str instance, int found
Summary
In this tutorial, we learned join() method of Strings in Python that is used to return the result when a string is concatenated with an iterable. We also saw its parameters and returned values; we had used different iterables like list,set,tuple dictionary in our examples also followed by a Live Example.










