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:

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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)

chevron_right


Output:

1-2-3-4

Joining with empty string.

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output:

geeks

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.

My Personal Notes arrow_drop_up

Image
Striver(underscore)79 at Codechef and codeforces D

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.