Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22466

Python for Beginners: Remove Quotes From a String in Python

$
0
0

Due to the availability of various modules, Python is one of the most used programming languages for natural language processing and text analytics. We use strings in python to analyze text data. Single quotes or double quotes enclose each string in Python. However, the input string may contain quotes in between. This article discusses different ways to remove quotes from a string in Python.

Remove Quotes From a String in Python Using For loop

In Python, we use for loop to iterate over an iterable object like a string or list. To remove quotes from a string using a for loop, we will use the following steps.

  • First, we will create a list named quotes to store the single quote and double quote characters.
  • Then, we will create an empty string named newStr to store the output string.
  • Now, we will use the for loop to iterate through the characters in the input string.
  • While iteration, if we find characters other than a single quote or double quotes, we will append the characters to newStr using the string concatenation operator. To check if a character is a single or double quote, we will use the membership operator.
  • If we find a single quote or double quote character in the string, we will skip it.
  • After execution of the for loop, we will get the output string in the variable newStr.

You can observe the entire process in the following example.

input_string = "Pythonf'orb''eginn'er's"
print("The input string is:", input_string)
quotes = ["'", '"']
newStr = ""
for character in input_string:
    if character not in quotes:
        newStr += character
print("The output string is:", newStr)

Output:

The input string is: Pythonf'orb''eginn'er's
The output string is: Pythonforbeginners

Instead of using the list named quotes and the membership operator, we can directly compare the characters for the presence of single quotes and double quotes in the string. For this, we will compare the characters using the equality operator in the if statement. The rest of the processes remain the same as above.

input_string = "Pythonf'orb''eginn'er's"
print("The input string is:", input_string)
newStr = ""
for character in input_string:
    if character == "'" or character == '"':
        continue
    else:
        newStr += character
print("The output string is:", newStr)

Output:

The input string is: Pythonf'orb''eginn'er's
The output string is: Pythonforbeginners

Here, you can see that we haven’t used the list and membership operator. Instead, we have directly compared the characters in the if statement using the equality operator.

Using the filter() Function and the join() Method to Remove Quotes From a String in Python

The filter() function is used to exclude elements from an iterable object. The syntax of the filter() function is as follows.

filter(input_function,iterable_object)

Here,

  • The iterable_object is the python object from which we need to exclude the elements.
  • The input_function is a function that takes an element of the iterable_object and returns True or False.

After execution, the filter() function returns a filter object. A filter object is an iterable object that contains all the elements of iterable_object for which the input_function returns True.

The join() method is used to create a string from a given iterable object. When invoked on a separator string, the join() method takes an iterable object as its input argument.

After execution, it returns a string in which all the elements of the iterable object are separated by the separator.
To remove quotes from a string in Python using the join() method and filter() function, we will use the following steps.

  • First, we will create a function isNotQuotes() that takes a character as its input argument. If the character is a single quote or double quotes character, it returns False. Otherwise, it returns True.
  • After this, we will use the filter() function to exclude quotes from the string. For this, we will pass the isNotQuotes function as the first input argument and the input string as the second argument to the filter function. After execution of the filter() function, we will get a filter object containing all the characters of the input string except quotes.
  • Now, we will use the join() method to obtain the output string. For this, we will use an empty string as a separator. We will invoke the join() method on the separator by passing the filter object as its input argument.

After execution of the join() method, we will get the output string without any quotes. You can observe this in the following example.

def isNotQuotes(character):
    if character == '"':
        return False
    if character == "'":
        return False
    return True


input_string = "Pythonf'orb''eginn'er's"
print("The input string is:", input_string)
filter_object=filter(isNotQuotes,input_string)
newStr = "".join(filter_object)
print("The output string is:", newStr)

Output:

The input string is: Pythonf'orb''eginn'er's
The output string is: Pythonforbeginners

Remove Quotes From a String in Python Using List Comprehension

List comprehension is used to create a new list from existing iterable objects. We can use list comprehension and the join() method to remove quotes from a string in Python. For this, we will use the following steps.

  • First, we will create a list named quotes to store the single quote and double quote characters.
  • Then, we will use list comprehension to get a list of characters from the input string excluding quotes.
  • Once we obtain the list of characters, we will invoke the join() method on an empty string and pass the list of characters as an input argument to it.
  • After execution of the join() method, we will get the desired string. You can observe this in the following example.
input_string = "Pythonf'orb''eginn'er's"
print("The input string is:", input_string)
quotes = ["'", '"']
myList = [character for character in input_string if character not in quotes]
newStr = "".join(myList)
print("The output string is:", newStr)

Output:

The input string is: Pythonf'orb''eginn'er's
The output string is: Pythonforbeginners

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

The replace() method is used to replace one character with another in a string. When invoked on a string, the replace() method takes the element that needs to be replaced as the first input argument and the new character as its second argument. After execution, it returns the modified string.
To remove quotes from a string in Python using the replace() method, we will use the following steps.

  • First, we will invoke the replace() method on the input string. Here, we will pass the single quote character as the first input argument and an empty string as the second input argument to the replace() method. The replace() method will return a string say tempStr as its output.
  • Again, we will invoke the replace() method on tempStr. This time, we will pass the double quote characters as the first input argument and an empty string as the second input argument to the replace() method.

After the second execution of the replace method, we will get the desired output string with no quote characters in it. You can observe this in the following example.

input_string = "Pythonf'orb''eginn'er's"
print("The input string is:", input_string)
tempStr = input_string.replace("'", "")
newStr = tempStr.replace('"', "")
print("The output string is:", newStr)

Output:

The input string is: Pythonf'orb''eginn'er's
The output string is: Pythonforbeginners

Remove Quotes From a String in Python Using re.sub() Function

Regular expressions provide us with various functions for string manipulation. We can use the re.sub() function to remove quotes from a string in Python.
The re.sub() function has the following syntax.

re.sub(old_character, new_character, input_string)

Here,

  • The input_string is the string from which we have to replace or remove characters.
  • The old_character is the character that needs to be removed from input_string.
  • The new_character is the character that will be inserted into the input_string in place of the old_character.

To remove quotes from a given string in python using the re.sub() function, we will use the single quote and double quotes characters as old_character and an empty string as the new_character.

  • First, we will pass a single quote as the first input argument, an empty string as the second input argument, and the given string as the third input argument to the re.sub() function. After execution, the sub() function will return a string. We will name it tempStr.
  • Now, we will pass double quotes as the first input argument, an empty string as the second input argument, and tempStr as the third input argument to the re.sub() function.

After execution, the re.sub() function will return the desired string having no quotation marks. You can observe this in the following code.

import re

input_string = "Pythonf'orb''eginn'er's"
print("The input string is:", input_string)
tempStr = re.sub("'", "", input_string)
newStr = re.sub('"', "", tempStr)
print("The output string is:", newStr)

Output:

The input string is: Pythonf'orb''eginn'er's
The output string is: Pythonforbeginners

Remove First and Last Quote characters From a Python String

If we have a string having quote characters only at the start and end as in "'Aditya'" or '"Aditya"', we can use the following approaches to remove quotes from the string.

Using the ast module to Remove First and Last Quote characters From a String

The ast module provides us with the literal_eval() function to evaluate an expression written in the form of a string. The literal_eval() function takes a string as its input argument, evaluates it, and returns the output.
As we pass the string '"Aditya"' or "'Aditya'"to the literal_eval() function, it considers the input string as expression and "Aditya" or 'Aditya' as the corresponding values. You can observe this in the following example.

import ast

input_string = "'Aditya'"
newStr=ast.literal_eval(input_string)
print("The input string is:", input_string)
print("The output string is:", newStr)

Output:

The input string is: 'Aditya'
The output string is: Aditya

This approach won’t work if the input string contains extra quotes at a position different than the first and last positions. If you try to remove quotes from such an input string using the literal_eval() function, the program will run into the SyntaxError as shown in the following example.

import ast

input_string = "'Adity'a'"
print("The input string is:", input_string)
newStr=ast.literal_eval(input_string)
print("The output string is:", newStr)

Output:

The input string is: 'Adity'a'
Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 5, in <module>
    newStr=ast.literal_eval(input_string)
  File "/usr/lib/python3.8/ast.py", line 59, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/lib/python3.8/ast.py", line 47, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    'Adity'a'
           ^
SyntaxError: invalid syntax

In this approach, the input string must contain quote characters at both the first and last positions.

If we have a quote character at the first position and not at the last position, the program will run into the SyntaxError . Similarly, If we have a quote character at the last position and not at the first position, the program will again run into a SyntaxError.
You can observe this in the following example.

import ast

input_string = "'Aditya"
print("The input string is:", input_string)
newStr=ast.literal_eval(input_string)
print("The output string is:", newStr)

Output:

The input string is: 'Aditya
Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 5, in <module>
    newStr=ast.literal_eval(input_string)
  File "/usr/lib/python3.8/ast.py", line 59, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/lib/python3.8/ast.py", line 47, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    'Aditya
          ^
SyntaxError: EOL while scanning string literal

Another condition to keep in mind is that the quote characters at both the start and end of the string should be the same. If there is a single quote at the start and double quotes at the end of the string or vice versa, the program will again run into the SyntaxError as shown below.

import ast

input_string = "'Aditya\""
print("The input string is:", input_string)
newStr=ast.literal_eval(input_string)
print("The output string is:", newStr)

Output:

The input string is: 'Aditya"
Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 5, in <module>
    newStr=ast.literal_eval(input_string)
  File "/usr/lib/python3.8/ast.py", line 59, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/lib/python3.8/ast.py", line 47, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    'Aditya"
           ^
SyntaxError: EOL while scanning string literal

The program will run into a ValueError exception if the input string doesn’t contain quote characters at the start and end. You can observe this in the following example.

import ast

input_string = "Aditya"
print("The input string is:", input_string)
newStr = ast.literal_eval(input_string)
print("The output string is:", newStr)

Output:

The input string is: Aditya
Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 5, in <module>
    newStr = ast.literal_eval(input_string)
  File "/usr/lib/python3.8/ast.py", line 99, in literal_eval
    return _convert(node_or_string)
  File "/usr/lib/python3.8/ast.py", line 98, in _convert
    return _convert_signed_num(node)
  File "/usr/lib/python3.8/ast.py", line 75, in _convert_signed_num
    return _convert_num(node)
  File "/usr/lib/python3.8/ast.py", line 66, in _convert_num
    _raise_malformed_node(node)
  File "/usr/lib/python3.8/ast.py", line 63, in _raise_malformed_node
    raise ValueError(f'malformed node or string: {node!r}')
ValueError: malformed node or string: <_ast.Name object at 0x7ffbe7ec60d0>

Hence, you should keep in mind that you can use the literal_eval() function only if the string contains quote characters only at the start and end.

Remove First and Last Quote characters From a String Using the eval() function

The eval() function works in a similar manner to the literal_eval() function. It also takes a string expression as an input argument, evaluates the expression, and returns the resultant value.
You can remove the first and last quotes from a string using the eval() function as shown below.

input_string = "'Aditya'"
print("The input string is:", input_string)
newStr = eval(input_string)
print("The output string is:", newStr)

Output:

The input string is: 'Aditya'
The output string is: Aditya

All the conditions mentioned for the string_eval() function are true for the eval() function. Hence, you should keep in mind that you cannot use the eval() function for every string.

Using the json module to Remove First and Last Quote characters From a String

A python string enclosed in double quotes is a valid json object. Therefore, we can use the json module to remove quotes from the first and last position of an input string.
The loads() function in the json module takes a json object as its input argument and returns a python string corresponding to the json object.
As our string contains extra quotes at the first and the last position. It will be considered a valid json object. Hence, we can pass it to the loads() function to obtain the output string as shown below.

import json
input_string = '"Aditya"'
print("The input string is:", input_string)
newStr = json.loads(input_string)
print("The output string is:", newStr)

Output:

The input string is: "Aditya"
The output string is: Aditya

Here, you should keep in mind that the string with single quotes is not a valid json string. Hence, if you try to remove single quotes from a given string using the json module, the program will run into error as shown in the following example.

import json
input_string = "'Aditya'"
print("The input string is:", input_string)
newStr = json.loads(input_string)
print("The output string is:", newStr)

Output:

The input string is: 'Aditya'
Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/string12.py", line 4, in <module>
    newStr = json.loads(input_string)
  File "/usr/lib/python3.8/json/__init__.py", line 357, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.8/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.8/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

In all the other situations except the extra double quotes, the approach using the json module will run into error if you try to remove the quotes from the string. Hence, you should keep in mind that you can use this approach in only one case.

Remove First and Last Quote characters From a String Using the strip() Function

Using the ast module, the json module, or the eval() function to remove quotes from the start and end of a string have many restrictions. It is highly possible that your program will run into an exception while using these approaches. Instead of the above approaches, we can use the strip() method to remove quotes from the start and end of a string.
The strip() method, when invoked on a string, takes a character as its input argument. After execution, it removes all the occurrences of the character from the start and the end of the string and returns a new string.
To remove quotes from the start and end of the string, we will use the following approach.

  • First, we will declare a string temp1, and initialize it to the input string.
  • Now, we will use a while loop to remove the quote characters. Inside the while loop, we will use the following steps.
  • First, we will declare a temporary string named temp2 and initialize it to temp1.
  • Now, we will invoke the strip() method on temp1. Here, we will pass a single quote character as an input argument to the strip() method. We will store the return value of the strip() method in temp3.
  • Again, we will invoke the strip() method on temp3. This time, we will pass double quotes as an input argument to the strip() method. We will store the output in temp4.
  • Now, we will check if temp4 is equal to temp2. If yes, all the quotes have been removed from the string as no change has occurred to the string in the current iteration. Hence, we will move out of the while loop using a break statement.
  • If temp2 is not equal to temp4, the string still contains quotes at the start and the end. Hence we need another iteration. For this, we will assign temp4 to temp1.

After execution of the while loop, we will get the desired string with quotes removed from its start and end. You can observe this in the following code.

input_string = "'Pythonforbeginners'"
print("The input string is:", input_string)
temp1 = input_string
while True:
    temp2 = temp1
    tem3 = temp2.strip("'")
    temp4 = tem3.strip('"')
    if temp4 == temp2:
        newStr = temp2
        print("The output string is:", newStr)
        break
    else:
        temp1 = temp4

Output:

The input string is: 'Pythonforbeginners'
The output string is: Pythonforbeginners

This approach removes the quotes successfully in the cases where the literal_eval() method and the eval() function fails. Therefore, you can use this approach freely. For instance, look at the following example.

input_string = "'''''''Pythonforbeginners'\""
print("The input string is:", input_string)
temp1 = input_string
while True:
    temp2 = temp1
    tem3 = temp2.strip("'")
    temp4 = tem3.strip('"')
    if temp4 == temp2:
        newStr = temp2
        print("The output string is:", newStr)
        break
    else:
        temp1 = temp4

Output:

The input string is: '''''''Pythonforbeginners'"
The output string is: Pythonforbeginners

In the above example, you can observe that we have used an input string that contains seven single quotes at its left and a single quote with double quotes at its right. Even in this asymmetrical situation, the program works correctly and doesn’t run into any Error.

Conclusion

In this article, we have discussed various approaches to remove quotes from a string in Python. Out of all these approaches, I will suggest you use the replace() method and the re.sub() function. The approaches using these functions are the most efficient.
I hope you enjoyed reading this article. To know more about python programming, you can read this article on dictionary comprehension in Python. You might also like this article on regression in machine learning.

The post Remove Quotes From a String in Python appeared first on PythonForBeginners.com.


Viewing all articles
Browse latest Browse all 22466

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>