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.

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.

Strings in Python

Strings in Python are basically, words we put between two quotation marks ‘ ‘ or ” “. There are two methods of representing these strings:

  1. Single quote [‘ ‘]
  2. Double quotes [” “]

If we need to write ‘It’s Monday’ we cannot write it using single quotes as the compiler is gonna read the quotation after it as the ending quotation and show an error as the last quote would not be having a starting one, which will make the quotations unbalanced. So, in such circumstances, we use the Double quotes method.

When it comes to choosing a method, it totally depends on the programmer to programmer. Generally, using double quotes for string representation and single quotes are used for regular expressions, dict keys or SQL is preferred. Hence both single quote and double quotes depict string in python but it’s sometimes our need to use one type over the other.

For example:

I/P-

 print("Hello 'Python'")


O/P- Hello ‘Python’

I/P-

print('"Python"')


O/P- “Python”

I/P-


O/P- Hello “Python”

print('Hello "Python"')

Assign String to a Variable

I/P-

a= "Python"
print(a)

O/P-

Python

Multiline strings:


We can print multiple lines in a single print statement, just by using 3 single or 3 double quotes at the starting and the end. The 3 quotation marks are basically making the text inside stay as it is and not hampering the look of the words.
For example

I/P-

a = """Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua."""
print(a)

or

a = '''Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.'''
print(a)
O/P- Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor incididunt
ut labore et dolore magna aliqua.

Printing strings with the help of arrays

Arrays are a very useful data structure in the lives of a programmer. They can be used in many ways, for instance here it is used for getting a particular part of the code.

I/P-

a = "Hello, World!"
print(a[1])

O/P-

e

String Slicing

This is also a way of removing some of the unwanted part of a code, which sometimes might include the quotations.

I/P-

b = "Hello, World!"
print(b[2:5])

O/P-

llo

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:

  1. I/P-
a_str=' " ab " cd " '
stripped_str=a_str.strip(' " " ')
print(stripped_str)

O/P-

ab " cd

Removing quotes from a string in python Using replace

2.

I/P-

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

O/P-

a b c d

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

I/P-

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

O/P:

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

I/P-

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

O/P-

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

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

I/P-

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)))))

O/P-

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

4. Using translate method To Print Without Quotes In Python

I/P-

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))

O/P-

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

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 during coding

ln= re.split("\s+" , string.strip(line)).replace(' \ " ' , ' ' )
a=ln[0,1,2,3...]

Must Read

Conclusion: Remove Quotes From a String in Python

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!

5 1 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments