Python String Join() Method

String Join() Method

In Python, When elements of a sequence are separated by a string separator, Python’s built-in join() method is used to connect the segments. In order to create a string, this function connects the components of a sequence.

join in python

Python Strings Join() 

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 :

Run
#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:

Run
#python Program 
list1 = ['P','r','e','p','s','I','n','s','t','a']
s = ""
s = s.join(list1)
print(s)

Output :

  PrepsInsta

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription