join() function in Python
The join() method is a string method and returns a string in which the elements of sequence have been joined by str separator.
Syntax:
string_name.join(iterable)
string_name: It is the name of string in which
joined elements of iterable will be
stored.
Parameters: The join() method takes iterable – objects capable of returning its members one at a time. Some examples are List, Tuple, String, Dictionary and Set
Return Value: The join() method returns a string concatenated with the elements of iterable.
Type Error: If the iterable contains any non-string values, it raises a TypeError exception.
Below program illustrates the working of join() method:
# Python program to demonstrate the # use of join function to join list # elements with a character. list1 = ['1','2','3','4'] s = "-" # joins elements of list1 by '-' # and stores in sting s s = s.join(list1) # join use to join a list of # strings to a separator s print(s) |
Output:
1-2-3-4
Joining with empty string.
# Python program to demonstrate the # use of join function to join list # elements without any separator. # Joining with empty separator list1 = ['g','e','e','k', 's'] print("".join(list1)) |
Output:
geeks
Recommended Posts:
- Python | Pandas str.join() to join string/list elements with passed delimiter
- Python | Join cycle in list
- Python | os.path.join() method
- numpy string operations | join() function
- Python | Join tuple elements in a list
- Python program to split and join a string
- Python | Merge, Join and Concatenate DataFrames using Panda
- Python | Ways to join pair of elements in list
- Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
- Join two text columns into a single column in Pandas
- Python map() function
- Python | hex() function
- Python | How to get function name ?
- Python | now() function
- Python | dir() function
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



