Python Join Function
"Join is a function in Python, that returns a string by joining the elements of an iterable, using a string or character of our choice."
In the case of join function, the iterable can be a list, dictionary, set, tuple, or even a string itself. The string that separates the iterations could be anything. It could just be a comma or a full-length string. We can even use a blank space or newline character (/n ) instead of a string.
The syntax of the join() method is:
the string is the name of string in which joined elements of iterable will be stored.
Note: If the iterable contains any non-string values, join() will raise a TypeError exception.
The implementation over the list iterable example is explained below. Here we join the elements of a list using a delimiter. A delimiter can be any character or nothing.
1, 2, 3, 4
With the join function, we can join two strings together, changing it into a larger string. Along with the iterable, we also have to use a string between each iterable and a string on its own is also iterable; thus, two strings can also be joined by using the join function.
It's is a lot easier and more compact than using a loop. We use a variable for iteration along with a string or fstring to print all the elements onto the screen.
How will the join function work in case of a "dictionary"? Are there any limitations to join() function?
The join function has certain limitations. We must have a question in our mind that how join function will work in case of a "dictionary" where there are values along with the keys. In the case of the dictionary, the join function will only return the key part, separated by the string in between, leaving the value side behind.
As we are on the subject, let us discuss another limitation associated with the join method. In situations where the iterable consists of a multi-data type, such as a list or tuple consisting of all integer variables and one single, double variable, the join function will not work. Instead, it will display an error. For join to function properly, all the variables should have the same sort of data type, either it is an integer, string, or any other.
Traceback (most recent call last): File "./prog.py", line 3, in TypeError: sequence item 1: expected str instance, int found