Remove single quotes from string python list

Treat the text as a row from a CSV formatted file:

import csv
import StringIO

result = '"testing","0.8841","642000.0","80.014521","-60.940653","4522126666","1500854400","","1500842014000","name","80.014521","-60.996532","sampledevice","3","name"'
print next(csv.reader(StringIO.StringIO(result)))

Giving you:

['testing', '0.8841', '642000.0', '80.014521', '-60.940653', '4522126666', '1500854400', '', '1500842014000', 'name', '80.014521', '-60.996532', 'sampledevice', '3', 'name']

Python's StringIO() function allows the text to be treated like a file allowing it to be passed to Python's CSV parser which is designed for parsing CSV files in this format. It can then correctly parse the text and return a list of items.


The returned data could then be further processed if needed to convert the text into numbers, i.e. integers or floats as appropriate. For example:

import csv
import StringIO

def convert(text):
    try:
        return int(text)
    except ValueError:
        pass

    try:
        return float(text)
    except ValueError:
        return text


result = '"testing","0.8841","642000.0","80.014521","-60.940653","4522126666","1500854400","","1500842014000","name","80.014521","-60.996532","sampledevice","3","name"'
values = [convert(value) for value in next(csv.reader(StringIO.StringIO(result)))]

print values

This would then return a list as follows:

['testing', 0.8841, 642000.0, 80.014521, -60.940653, 4522126666L, 1500854400, '', 1500842014000L, 'name', 80.014521, -60.996532, 'sampledevice', 3, 'name']

You can remove single quotes from a string in python using replace the function or translate function, Regular Expressions, etc.

A easiest way is just replace the apostrophe character with an empty string:

msg = "Hell'o"
print(msg.replace("'", ""))
  • replace() function
  • translate() function
  • Regular Expressions
  • Other ways – join function etc.

Examples

Here are a few ways of removing a single ' from a string in python.

1. replace() function (str.replace)

replace() function is usually used to return a string with all the instances of the substring replaced.

msg = "A single 'char'".replace("'", "")
print(msg)

Output: A single char

2. translate() function (str.translate)

This method is used in Python 2, To remove characters you can pass the first argument to the function with all the substrings to be removed as second.

"A single ' char".translate(None,"'")

In Python 3

You will have to use str.maketrans

3. Regular Expressions using re

Regular Expressions using re are even more powerful (but slow) and can be used to replace characters that match a particular regex rather than a substring.

You have to import a module in python.

import re

re.sub("'", "", "A single ' char")

4. Other ways – remove single quotes in Python

Using list call along with remove and join. But it will remove only first occurrence of single quote.

str = "Hello' ' word"
x = list(str)
x.remove("'")
print(''.join(x))

Output: Hello ‘ word

Do comment if you know any other way to do it, doubts and suggestion on this tutorial.

Note: This example (Project) is developed in PyCharm 2020.1 (Community Edition)
JRE: 1.8.0
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
macOS 10.15.4
Python 3.7
All Python Programs code are in Python 3, so it may change its different from python 2 or upgraded versions.

Remove single quotes from string python list

Degree in Computer Science and Engineer: App Developer and has multiple Programming languages experience. Enthusiasm for technology & like learning technical.

Python is the best programming language for making complex code into simpler code. And it’s due to High Level and Just like English language programming language. For example, you want to replace a single quote in the string. If you will use the other programming language you may find difficulty. But in the case of Python, it’s very easy. In this entire tutorial, you will know the various method to replace a single quote from the string using python.

Before going to the coding demonstration part you should make sure that you do all the coding parts on the Jupyter Notebook. As I am also doing on it, so it makes you able to understand more about this tutorial.

Method 1 : Using the replace() method

Python provides an inbuilt function for a quicker solution to the problem. The replace() method is on of them. To replace a single quote from the string you will pass the two parameters. The first is the string you want to replace and the other is the string you want to place. In our case it is string.replace(” ‘ “,” “).

Execute the below lines of code.

string = "Hello! Data Science' Learner"
string.replace("'","")

Output

Remove single quotes from string python list
Replacing a single quote in python using the replace method

Method 2: Using maketrans()/translate() method

There are also other functions to replace a single quote from a string in python. It is the maketrans() and translate(). Inside the translate() function you will pass the key-value pair with the {}. The key will be the string you want to replace and the value will be the string to place. After that, you will make its result as an argument for the maketrans()function.

Run the below lines of code.

string = "Hello! Data Science' Learner"
string.translate(string.maketrans({"'":None}))

Output

Remove single quotes from string python list
Replacing single quote in python using themaketrans() and translate() method

Method 3: Replace a single quote in python using Regex

The third method to replace a single quote is the use of the regex module. Regex is one of the basic building blocks in Text Mining and Analytics. For this example, you have to use the sub()function that accepts three parameters. One is the string you want to replace, the second is the string you want to place and the third is the input string.

Execute the below lines of code to replace a single quote in python.

import re
string = "Hello! Data Science' Learner"
re.sub("'","",string)

Output

Remove single quotes from string python list
Replacing single quote using regex method

Method 4: Using the list

You can also use the list to replace the single quote. You have to first convert the input string to a list and then remove the single quote from the list. At last, you have to join each list value to get the final string.

string = "Hello! Data Science' Learner"
s = list(string)
s.remove("'")
''.join(s)

Output

Remove single quotes from string python list
Replacing a single quote using the list

Conclusion

Replacing some strings is a must if you pre-processing the datasets. However, in this tutorial, the implementation is for only understanding the concept. These are the methods to replace a single quote in a string using python. You can use these methods to replace anything from the string.

I hope you have liked this tutorial. If you are facing any difficulties then you can contact us for more help.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

We respect your privacy and take protecting it seriously

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

How do I remove single quotes from a list in Python?

Use str. replace(old, new) with old as "'" and new as "" to remove all single quotes from the string.

How do you remove quotes from a string in a list Python?

To remove the quotes from a list of strings:.
Use a list comprehension to iterate over the list..
Use the str. strip() method to remove the leading and trailing quotes from each string..
The items in the new list won't contain leading and trailing quotes..

How do you replace a single quote in a string?

Use the String. replace() method to replace single with double quotes, e.g. const replaced = str. replace(/'/g, " ); . The replace method will return a new string where all occurrences of single quotes are replaced with double quotes.

How do you remove quotes from a string?

To remove double quotes just from the beginning and end of the String, we can use a more specific regular expression: String result = input. replaceAll("^\"|\"$", ""); After executing this example, occurrences of double quotes at the beginning or at end of the String will be replaced by empty strings.

Tải thêm tài liệu liên quan đến bài viết Remove single quotes from string python list