Python :: Concatenating Strings

When you need to concatenate a list of strings, you can do this using a for loop by adding each element one by one. However, this would be very inefficient, especially if the list is long. In Python, strings are immutable, and thus the left and right strings would have to be copied into the new string for every pair of concatenation.

A better approach is to use the join() function as shown below:

characters = ['p', 'y', 't', 'h', 'o', 'n']
word = "".join(characters)
print(word) # python

Leave a Reply

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

*