MOCKSTACKS
EN
Questions And Answers

More Tutorials









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:
string.join(iterable)

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.

For Example:
#join() with lists
numList = ['1', '2', '3', '4']
separator = ', '
print(separator.join(numList))

Output

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.

For Example:
myDictionary = {"name": "Jack", "country": "America"}
separator = "_separator_"
print(separator.join(myDictionary))

Output

name_separator_country

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.

For Example:
inputlist = ["Test1",13,"Test2",24,"Test3",100,"Test4"]
sep = '_'
out = sep.join(inputlist)
print(out)

Output

Traceback (most recent call last): File "./prog.py", line 3, in TypeError: sequence item 1: expected str instance, int found


Example


lis = ["John", "cena", "Randy", "orton",
       "Sheamus", "khali", "jinder mahal"]

# for item in lis:
#     print(item, "and", end=" ")

a = ", ".join(lis)
print(a, "other wwe superstars")


Conclusion

In this page (written and validated by ) you learned about Python Join Function . What's Next? If you are interested in completing Python tutorial, your next topic will be learning about: Python Map Filter and Reduce.



Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue: mockstacks@gmail.com.


Share On:


Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using Mockstacks.com, you agree to have read and accepted our terms of use, cookies and privacy policy.