Remove Quotes From String in Python

  1. Remove Quotes From String in Python Using the replace() Method
  2. Remove Quotes From String in Python Using the strip() Method
  3. Remove Quotes From String in Python Using the lstrip() Method
  4. Remove Quotes From String in Python Using the rstrip() Method
  5. Remove Quotes From String in Python Using the literal_eval() Method

A combination of characters enclosed in a single '' or double " quotes is called a string. This article will introduce different methods to remove quotes from a string in Python.

Remove Quotes From String in Python Using the replace() Method

This method takes 2 arguments, which could be named as old and new. We can call the replace(), with '""' as the old string and "" (empty string) as the new string, to remove all quotes.

The complete example code is as follows:

old_string= '"python"'

new_string=old_string.replace('"','')

print("The original string is - {}".format(old_string))
print("The converted string is - {}".format(new_string))

Output:

The original string is - "python"
The converted string is - python

Remove Quotes From String in Python Using the strip() Method

In this method, quotes are removed from both ends of the string. Quotes '""' is passed as an argument in this function, and it will remove quotes on the old string from both sides and generate new_string without quotes.

The complete example code is as follows:

old_string= '"python"'

new_string=old_string.strip('"')

print("The original string is - {}".format(old_string))
print("The converted string is - {}".format(new_string))

Output:

The original string is - "python"
The converted string is - python

Remove Quotes From String in Python Using the lstrip() Method

This method will remove quotes if they appear at the start of the string. It is applicable in case you need to remove quotes at the start of the string.

The complete example code is as follows:

old_string= '"python'

new_string=old_string.lstrip('\"')

print("The original string is - {}".format(old_string))
print("The converted string is - {}".format(new_string))

Output:

The original string is - "python
The converted string is - python

Remove Quotes From String in Python Using the rstrip() Method

This method will remove quotes if they appear at the end of the string. The default trailing character to be removed when no parameter is passed is the white space.

The complete example code is as follows.

old_string= 'python"'
new_string=old_string.rstrip('\"')

print("The original string is - {}".format(old_string))
print("The converted string is - {}".format(new_string))

Output:

The original string is - python"
The converted string is - python

Remove Quotes From String in Python Using the literal_eval() Method

This method will test a Python literal or container view expression node, Unicode or Latin-1 encoded string. The supplied string or node may only consist of the following literal Python structures: strings, numbers, tuples, lists, dictionaries, booleans, etc. It securely tests strings containing untrusted Python values without having to examine values themselves.

The complete example code is as follows:

string="'Python Programming'"

output=eval(string)

print(output)

Output:

Python Programming
Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Python String

  • Convert Integer to String in Python
  • Remove Special Characters From the String in Python