How To Join Words In a List Using Python Code Example

Do you have a list of words in Python and want to join those words to make a sentence? In this article, you will learn how to join Words in a List using Python.

You can join the words present in a list naively in two ways. First using the join Method present in the String class of the Python and another one is by using the for loop to join each word to an empty string to create a sentence.

Using join() Method to Join Words In a List Using Python

The string class in Python comes with an inbuilt function called join[1] that is used to concatenate the elements of a list into a single string variable or sentence.

Take all the strings in the iterable and concatenate them into one long string. If the iterable contains any non-string values, such as bytes objects, a TypeError will be thrown. In this manner, the string acts as a comma-delimited separator.

Let us see in the below example code the usage of the join() method to join words in a list using Python.

#Initializing the a List of Words
listOfWords = ["I", "Want", "To", "Join", "This", "String."]

#Initializing the Empty String to Save the Joined String
joinedString = ""

#using Join to join the words from a list of Words.
#Since we need space between the Words
#hence I have Added space in between.
joinedString += ' '.join(listOfWords)

#Printing the joined String
print(joinedString)

Output:

I Want To Join This String.

As you can see in the above code, using a space string with a join I was able to join the words of a List with a space in each word. If you do not want the space in between then you can remove the space and just use the quotes.

Joining the Words in a List with Hyphen

Let us see another code example in which if you want to join a list of words with a hyphen in between how you can do that using the join method.

#Initializing the a List of Words
listOfWords = ["I", "Want", "To", "Join", "This", "String", "With", "Hyphen"]

#Initializing the Empty String to Save the Joined String
joinedString = ""

#using Join to join the words from a list of Words.
#Since we need hyphen between the Words
#hence I have Added hyphen
joinedString += '-'.join(listOfWords)

#Printing the joined String
print(joinedString)

Output:

I-Want-To-Join-This-String-With-Hyphen

Similarly, you can use anything instead of a hyphen such as commas, colons, etc. to add in between the words to create a string.

How To Join Words In a List Using Python Code Example

Using For Loop to Join Words in a List Using Python

The second way that you can use to add the words present in a list using Python is using the for loop or any loop you know.

But if you want to add the space between the words then to make sure no space is added in the front or end of the final string we need to use the strip method to remove extra spaces from the front or back.

Let us see in the below example code the usage of For loop to join the words present in a list and the strip method to remove the trailing and leading spaces from the final string.

#Initializing the a List of Words
listOfWords = ["I", "Want", "To", "Join", "This", "String."]

#Initializing the Empty String to Save the Joined String
joinedString = ""

#Using the For Loop and Strip to remove
#The Extra Spaces.
for word in listOfWords:
    joinedString += word + " ";

#removing extra space added in last
joinedString = joinedString.strip(" ")

#Printing the joined String
print(joinedString)

Output:

I Want To Join This String.

As you can see in the above code I was able to successfully join the words present in the list using the for Loop and the strip method which is used to remove the trailing and leading spaces from the final string having the joined words.

Wrap Up

I hope you got the answer on how to add words to a list using Python. I have discussed two methods with code examples and looking at the example I would suggest you use the first method which is the join method present in the string class.

That method is a faster and optimized way to join the words in a list. You can use it to join even numbers as strings in the complete string.

If you have any better method than the one discussed above then please let me know in the comment section and I will be happy to add it here.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Python Code Example: How To Find Element in a List in 6 Way
  2. How To Iterate Over Rows in a DataFrame in Pandas Python
  3. Python Get Filename From A Path Without Extension 4 Ways
  4. How To Concatenate String and Integer In Python
  5. Code Example: Convert a String to DateTime in Python
  6. How Do I Sort A Dictionary By Value In Python
  7. Code Example: How To Add Numbers In A List Python

Leave a Comment