How to Remove Quotes From a String in Python

We often come across small issues that turn out to be big. While coding, small tasks sometimes become tedious when not handled well. One of those tasks is output formatting, in which we require to remove quotes from a string in Python. Let’s discuss certain ways in which we can remove quotes from a string in Python.

To Remove Quotes from a String, you can use replace() function or you can exclude it strip if the quotes appear at endpoints of strings. In this post, we’ll go through all the methods to remove the quotes from a string.

Before we learn how to remove quotes from strings, let us look at how to use quotes from a string in Python and the ways and syntaxes to do so.

Removing quotes from a string in python Using strip()

Removing quotes from the ends of a string leaves the quotes that may be in the middle of the string.
For example:

Code –

a_str = ' " ab " cd " '
stripped_str = a_str.strip(' " " ')
print(stripped_str)

Output –

ab " cd

Note: This will not remove the in-between quotes from the string. This method is only used when you want to strip the end quoptes from a string.

Most of the users use this method while reading the CSV file. In many cases, the large strings in CSV or other Excel files are formatted with double quotes on both ends.

Removing quotes from a string in python Using Replace

Code –

a_str=' " a b " c d " '
replaced_str= a_str.replace(' " ' , " ")
print(replaced_str)

Output –

a b c d

This method removes all the occurrences of double quotes (“) from a string. Make sure that you use single quotes (‘) to enclose double quotes in replace() function. Otherwise, it’ll throw an error.

Ways to print Strings in Python without quotes

Whenever we print a list or string in Python, we generally use str( ) because of which we have single quotes in the output we get. Suppose if the problem requires to print solution without quotes. Here are some ways to print lists/strings without the use of quotes.

1. Using map() method to Print without quotes in Python

Code –

ini_list =['a', 'b', 'c', 'd']
print("List with str", str(ini_list))
print("List in proper method", '[%s]'%', '.join(map(str, ini_list)))

Output –

List with str['a', 'b', 'c', 'd']
List in proper method[a, b, c, d]

2. Using sep() method To Print Without Quotes In Python

Code –

ini_list= ['a' , 'b' , 'c' , 'd']
print("list with str: " , str(ini_list))
print(*ini_list, sep = ' , ')

Output –

list with str: ['a' , 'b' , 'c' , 'd']
a, b, c, d

3. Using .format() method To Print Without Quotes In Python

Code –

ini_list= ['a' , 'b' , 'c' , 'd']
print(" List with str", str(ini_list))
print("printing list without quotes" , ("[{0}]".format(' , ' . join(map(str, ini_list)))))

Output –

List with str ['a' , 'b' , 'c' , 'd']

4. Using the translate method To Print Without Quotes In Python

Input –

ini_list=['a' , 'b' , 'c' , 'd']
print("list with str:", str(ini_list))
translation= {39: None}
print("printing list without quotes", str(ini_list).translate(translation))

Output –

list with str : ['a' , 'b' , 'c' , 'd']
printing list without quotes [a, b, c, d]

Also, Read | Print Blank Line in Python

Removing quotes from a Text File in Python

To remove single or double quotations from a given Python text file, the following syntax can be used –

ln= re.split("\s+" , string.strip(line)).replace(' \ " ' , ' ' )
print(ln)

This method uses replace() method to find the exact matches for double quotes (“) and then replaces it with an empty string.

Must Read

Conclusion

Quotes while being important sometimes tend to spoil the look of certain outputs, for more well-put outputs we remove the quotations, which is just as simple as it sounds but makes a lot of difference in the output and user. It can be done in any of the above-shown ways. Hope this helped.

Still have any doubts or questions, do let me know in the comment section below. I will try to help you as soon as possible.

Happy Pythoning!

Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anne
Anne
1 year ago

Use the join method.

>>> your_list = ['1', '2', '3']
>>> print(', '.join(your_list))
1, 2, 3
Pratik Kinage
Admin
1 year ago
Reply to  Anne

Your list doesn’t have quotes tho.