How To Join A List Of Strings Wrapping Each String In Quotes In Python

Join a list of strings wrapping each string in quotes in Python

If you want to understand how to join a list of strings wrapping each string in quotes in Python, let’s read this article. I will guide you on how to do that by using For loop and using the join() method.

Join a list of strings wrapping each string in quotes in Python

Use For loop

For loop is used a lot when working with arrays in this article, so we can also use it to join a list of strings and wrap each string in quotes.

Example:

words = ['learnshareit', 'hello', 'world']
str = ""

# Use For loop to Join string wrapping string in quotes
for item in words:
    if item == words[0]:
        str = str + item
    else:
        str = str +'", "' + item
        
print('"'+str+'"')

Output:

"learnshareit", "hello", "world"

In the above example, I have added the string (“, “) before each element of the array except the first element. After doing this, we will get a string of the form (item[0]”, “item[ 1]”, “…”, “item[n]), then we add two characters (“”) to enclose the generated string to display the string as we expect.

Use join() method

In the above example, we see that when we use the For loop, we can join a list of strings wrapping each string in quotes. However, it will be difficult to implement, so we should find another way to do it.

join() method can help us join the elements in the array with a string or a particular character we want, so to join a list of strings wrapping each string in quotes, we can also use the join() method.

Syntax:

string.join(iterable)

Parameter:

  • iterable: This is an iterable object.

Example:

words = ['learnshareit', 'hello', 'world']
str = '", "'.join(words)

print('"' + str + '"')

Output:

"learnshareit", "hello", "world"

In the above example, we see that the return result of the join() method is correct with the string concatenating between the elements as (“, “) and the object passed in the array ‘words’.

As shown in the above two methods, we manually add two (“) characters as the public at the ends. This makes your code confusing.

To fix it, I will use the format() method to add two (“).

Syntax:

string.format(value1, value2…)

Parameter:

  • value1, value2…: This is the string to be formatted.

Example:

words = ['learnshareit', 'hello', 'world']
str = ""

# Use For loop to Join string wrapping string in quotes
for item in words:
    if item == words[0]:
        str = str + item
    else:
        str = str +'", "' + item

# Use the join() method to Join string wrapping string in quotes
str1 = '", "'.join(words)

# Use the format() method to add '"' two ends
print('"{0}"'.format(str))
print('"{0}"'.format(str1))

Output:

"learnshareit", "hello", "world"
"learnshareit", "hello", "world"

I used the placeholder ‘{0}’ to represent it as str or str1, then formatted it in (“). The same result when we use it manually, but when using the format () method of our code will be more explicit in some cases of complex formatting. We can limit errors.

Summary

In the article, I have shown you how to Join a list of strings wrapping each string in quotes in Python in two ways. I usually prefer the latter because it makes our code more accessible and faster to run. Hope this article helps you and your article. Good luck!

Maybe you are interested:

Leave a Reply

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