| by Arround The Web | No comments

Remove Quotes From String Python

Python is a programming language used for text analytics and natural language processing. In Python, strings are utilized for analyzing text data. More specifically, a string may contain double or single quotes itself. However, sometimes, we have unexpected quotes within any string that requires dealing with them.

This post will discuss several methods to remove quotes from the Python string.

How to Remove Quotes From String in Python?

To delete the quotes from the string in Python, the below-described methods are used:

Method 1: Remove Quotes From String in Python Using “for” Loop

To eliminate the quotes from any Python string, the built-in iterative function “for” loop is used. It is utilized for iterating over a desired sequence. The specified data can be a set of instructions, string, list, or tuple. Additionally, it can generate a string without quotes in Python.

Example

Initially, generate a string variable and pass a string which contains quotes:

original_str = "Linux'Hint' is the 'World' Best Tutorial Website!!"

Create a variable that will take the double and single quotes:

check_quotes = ["'", '"']

Generate one more empty string:

Without_quotes = ""

Use the “for” loop which will check the character of the provided string one by one with the “if” statement. If the checked character is not a single or double quote, then it will pass to the previously created empty string:

for character in original_str:

if character not in check_quotes:

Without_quotes += character

Then, print string without quotes:

print(Without_quotes)

Output

Method 2: Remove Quotes From String in Python Using “replace()” Method

The “replace()” built-in method is used to delete the single or double quotes in any Python string. It can return a duplicate string where a substring’s presence is exchanged with another.

Example

Call the “replace()” method with required arguments as replacement value, the value which needs to be replaced, and pass it to the “second_string” variable:

second_string = original_str.replace('"','')

To get the modified string which contains only character, use the “print()” function:

print(second_string)

As you can see in the following output, the string has been displayed without quotes:

Method 3: Remove Quotes From String in Python Using “re.sub()” Method

The “re.sub()” method can be used for removing the quotes from the Python string that is the module of the regex library. This built-in method replaces all the characters that match the provided regular expression format with the specified argument.

Example

At first, import the “re” module:

import re

Use the “re.sub()” method that takes the value which needs to be replaced, the replaced value, and the original string as arguments. Then, store them in the “second_string” variable:

second_string = re.sub('"','',original_str)

Call the “print()” method to get the excluded quote string:

print(second_string)

Output

Method 4: Remove Quotes From String in Python Using “strip()” Method

Sometimes, you may want to remove the quotes from the starting and ending of any string in Python, the “strip()” method can be used. To do so, follow the below-stated example.

Example

First, create a variable string and pass a string with quotes at the starting and ending of it:

original_str = '"LinuxHint is World Best Tutorial Website!!"'

Use the “strip()” method with replacement value as an argument and save it to the “new_string” variable:

new_string=original_str.strip('"')

Print the filtered string:

print(second_string)

The below-given string in the output does not contain any quotes:

Method 5: Remove Quotes From String in Python Using “Istrip()” Method

If you only want to remove the quotes from the start Python string, the “Istrip()” method can also be used. Follow the below given an example for a better understanding.

Example

Create a new variable string and pass the value which contains a double quote at the start of the string:

original_str = '"LinuxHint is World Best Tutorial Website!!'

Call the “Istrip()” method and pass “\” as a parameter to delete the double quotes. Save them to the “new_string” variable:

new_string = original_str.lstrip('"')

To display the filtered string, use the “print()” statement:

print(new_string)

Output

Method 6: Remove Quotes From String in Python Using “rstrip()” Method

The “rstrip()” built-in method can be utilized to remove double quotes from the end of the string in Python. It is opposite to the “Istrip()” method and eliminates the provided element from the end of any desired string.

Example

Initially, generate the string which contains the double quotes at the end of it:

original_str = 'LinuxHint is World Best Tutorial Website!!"'

Use the “rstrip()” method and pass a “\” as an argument to remove the double quotes. Store them to the “new_string” variable:

new_string = original_str.rstrip('"')

Lastly, call the “print()” function to show the string:

print(new_string)

It can be observed that the below-stated output has no double quotes:

That’s all! We have elaborated on removing quotes from the Python string.

Conclusion

To eliminate the quotes from any Python string, the “for” Loop, the “replace()” method, the “re.sub()” method, the “strip()” method, the “Istrip()” method, and the “rstrip()” methods are used. All methods are Pythons’ built-in functions that are utilized for multiple purposes. This post illustrated multiple functions to remove the quotes from the Python string.

Share Button

Source: linuxhint.com

Leave a Reply