Python String join()

Filed Under: Python

Python String join() function returns a string that is the concatenation of the strings in iterable with string object as a delimiter.

Python String join()

Python String join() function syntax is:


str_object.join(iterable)

The iterable elements must be a string, otherwise TypeError will be raised.

The separator between elements is the string providing this method.

Python String join() example

Let’s look at some examples of string join() method.


s1 = ','.join(['a', 'b', 'c'])

print(s1)

Output: a,b,c

The string object can have multiple characters too.


s = 'abc'

s1 = s.join('xyz')

print(s1)

Output: xabcyabcz

Notice that string is iterable. Here iterable elements are ‘x’, ‘y’ and ‘z’. They are concatenated together with ‘abc’ as the separator i.e. ‘x’+’abc’+’y’+’abc’+’z’.

Concatenate Strings using join() function

We can concatenate a sequence of strings (list, tuple) using join() function. The string object should be an empty string.


s1 = ''.join(['a', 'b', 'c'])
print(s1)

s1 = ''.join(('a', 'b', 'c'))
print(s1)

Output:


abc
abc
You can checkout complete python script and more Python examples from our GitHub Repository.

Reference: API Doc

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages