Python String Join() Method

String Join() Method in Python :

The join() method in python is a string method and returns a string in which the elements of a sequence have been joined by a string separator. 

Example : list=[1,2,3,4] 

    • 1-2-3-4
String Join Method in Python

Syntax :

  string_name.join(iterable)
string_name: It is the name of string in which joined elements of iterable will be stored.

  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.

  • Let’s understand with some examples : 

Code #1 :

#python Program 
list1 = [‘P’,‘r’,‘e’,‘p’,‘s’,‘I’,‘n’,‘s’,‘t’,‘a’]  
  
s = “-“
s = s.join(list1) 
print(s) 

Output:

P-r-e-p-s-I-n-s-t-a

Code #2:

#python Program 
list1 = [‘P’,‘r’,‘e’,‘p’,‘s’,‘I’,‘n’,‘s’,‘t’,‘a’]  
  
s = “”
s = s.join(list1) 
print(s) 

Output :

  PrepsInsta