Python join string method

The Python join() is a method of string that can concantenate string in iterable.The main purpose of this function is to join together a string or characters.It cannot join any values other than string or character.

The format of this method is:

join( iterable )

Argument type

The argument type accepted by join() is iterable.An iterable are those object that can return one element/item or members at a time.Lists, tuples, strings, dictionary, are all iterables.

Return type

The function returns a concatenation of the strings in iterable.This means the function returns a string formed by joining all the items of the iterable object passed to the function as argument.

To sum it up,the purpose of join() is to return the string that is formed by joining all the elements obtained from the iterable object which is passed to join() as the argument.

If the join() method cannot concantenate the elements of the object due to non-string element type than it raises an exception of TypeError.We will see an example when join() raises such exception later.

Let us consider an example.

  
>>> ls=['N' , 'ew' , ' ' , 'String']
>>> st
>>> st.join( ls )
'New String'

The elements of ‘ls’ is taken and the join() combines them and gives them as the output.

Let us take another example where tuple is passed as the argument.

  
>>> tp=( 'Elephant' , '  fantasia' , ' Asia')
>>> st=''
>>> st.join( tp )
'Elephant  fantasia Asia'

Same case here.

Sometimes we also use an empty string instead of using any declared object to call the join() method ( and note only string type object can call the join() method).If the argument pass is a dictionary make sure that the key is string type else you will get an error.

>>> st=''
>>> dic={ '1':'New' , '2':' Text' , '3':' Fourth' }
>>> st.join( dic )
'123'
>>> #Using empty string
>>> ''.join( dic ) #Work fine!!
'123'

The ” means an empty string and it will behave like the ‘st’ except that ” has no name attached to it.


When there is non-string type elements in the object

The join() will raise an exception/error if there is any non-string type element in the object.The error raise is of TypeError type.

Consider the code example below.

>>> ls=['24' , 78 , '90']
>>> "".join( ls )
Traceback (most recent call last):
  File "<pyshell#41>", line 1, in <module>
    "".join( ls )
TypeError: sequence item 1: expected str instance, int found

At the last line do you see TypeError in “TypeError: sequence item 1: expected str instance, int found”.It is saying the elements expected from ls is of string type but the item 1 (referring to the item of ls with index 1 which is 78) is int (integer) type.Hence the join() cannot join the elements together.

Another example using dictionary.

>>> dic1={ '1':'One' , '2':'Two' , 3:'Three' , 4:'Four' }
>>> ''.join( dic1 )
Traceback (most recent call last):
  File "<pyshell#43>", line 1, in <module>
    ''.join( dic1 )
TypeError: sequence item 2: expected str instance, int found

The index 2 key element is an integer the 3rd key element also has integer but only 2nd index is mentioned.