Sunday, March 19, 2017

Function "join()" in Python

Function join() in Python 

In this post, with a simple example, the power of the "join" function is presented.

l = ['This', 'is', 'a', 'sentence']
str = ' '.join(l)
print(str + '.')

Result:
   This is a sentence.

In the above  example, the "join" function is used to join a list of words by space.

Note: Actually, "join" is a method of the String class that returns a string in which the string elements of sequence are joined by the instance as separator. In the above example, the String instance is a space character: '  '. So the above example, could be written as:


l = ['This', 'is', 'a', 'sentence']
space = ' '
str = space.join(l)
print(str + '.')



Copyright ©2017, Software Developer, All rights reserved.
See Contents

No comments:

Post a Comment