Why do we use join() function in Python?

The join() is defined as a string method which returns a string value. It is concatenated with the elements of an iterable. It provides a flexible way to concatenate the strings. See an example below.

Example:

  1. str = “Rohan”  
  2. str2 = “ab”  
  3. # Calling function    
  4. str2 = str.join(str2)    
  5. # Displaying result    
  6. print(str2)  

Output:

aRohanb