11-04-2022, 10:00 AM
How to Strip One Set of Double Quotes from Strings in Python
5/5 – (1 vote)
Problem Formulation and Solution Overview
When working with data, you may encounter a string or list of strings containing two (2) double quotes. This article shows you how to remove one set of these double quotes.
Question: How would we write code to remove the extra set of double quotes?
We can accomplish this task by one of the following options:
Method 1: Use startswith() and endswith()
This method uses and in conjunction with to remove one set of double quotes from a string.
The first line in the above code snippet declares a string containing two (2) sets of double quotes and saves this to the variable.
The following line calls the statement with the and functions. Both functions are passed the argument ().
This statement checks to see ifstarts with and ends with the above argument. If true, the code moves to the next line and, using , removes the specified character.
The results are output to the terminal.
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F-UkcLQxzPA4%2Fhqdefault.jpg)
Method 2: Use Regex
You can use the method to remove one set of double quotes from a string.
The first line in the above code snippet imports the library. This allows access to and manipulation of strings to extract the desired result.
The following line declares a string containing two (2) sets of double quotes and saves this to the variable.
The next line uses to search this string for any occurrences of double quotes, removes the same and saves the result back to . This overwrites the original string.
The results are output to the terminal.
Another option is to use and pass this function two (2) arguments:
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F3MtrUf81k6c%2Fhqdefault.jpg)
Method 3: Use replace()
This method uses https://blog.finxter.com/python-regex/ to remove one set of double quotes from a string.
The first line in the above code snippet declares a string containing two (2) sets of double quotes and saves this to the variable.
The following appends the function to and is passed two (2) arguments:
The results save back to. The results are output to the terminal.
Method 4: Use list()
This method passes a list containing double quotes to the list function. This option differs from the other options as it removes all double quotes.
The first line in the above code contains a list of Finxter user’s emails with double quotes at the beginning and the end.
This saves to.
The following line uses a list and passes to it as an argument. The results save back to and are output to the terminal.
Note: Converting a list containing double quotes to a list removes all double quotes.
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2FqXOFngx0bQU%2Fhqdefault.jpg)
Method 5: Use Pandas
This method uses Pandas to remove all double quotes from a CSV file.
Contents of CSV file
The above example imports the Pandas library. This library allows access to and manipulation of a Pandas DataFrame.
The following line reads in the CSV file, without the header row into the DataFrame,.
If output to the terminal, the DataFrame appears as follows:
|
0 |
1 |
2 |
3
0 |
Shop |
Category |
Product |
Number
1 |
Toronto”” |
Jeans”” |
10534″” |
15″”
2 |
Montreal”” |
Tops”” |
5415″” |
32″”
3 |
Ottawa”” |
Coats”” |
98341″” |
22″”
Note: By default, when the CSV file was imported, the beginning double quotes were removed, thus leaving the trailing double quotes.
Next, all occurrences of trailing quotes are replaced (removed) from the DataFrame.
The output of the modified DataFrame, are output to the terminal.
|
0 |
1 |
2 |
3
0 |
Shop |
Category |
Product |
Number
1 |
Toronto |
Jeans |
10534 |
15
2 |
Montreal |
Tops |
5415 |
32
3 |
Ottawa |
Coats |
98341 |
22
![[Image: lyteCache.php?origThumbUrl=https%3A%2F%2...efault.jpg]](https://blog.finxter.com/wp-content/plugins/wp-youtube-lyte/lyteCache.php?origThumbUrl=https%3A%2F%2Fi.ytimg.com%2Fvi%2F-JKVy_HliQE%2Fhqdefault.jpg)
Summary
This article has provided five (5) ways to remove one set of double quotes from a string and all double quotes to select the best fit for your coding requirements.
Good Luck & Happy Coding!
Programmer Humor – Blockchain
[i]“Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.” source – xkcd[/i]
5/5 – (1 vote)
Problem Formulation and Solution Overview
When working with data, you may encounter a string or list of strings containing two (2) double quotes. This article shows you how to remove one set of these double quotes.
Question: How would we write code to remove the extra set of double quotes?We can accomplish this task by one of the following options:
- Method 1: Use ,Code:
startswith()andCode:endswith()Code:slicing
- Method 2: Use Code:
regex
- Method 3: Use Code:
replace()
- Method 4: Use Code:
list()
- Method 5: Use Pandas
Method 1: Use startswith() and endswith()
This method uses
Code:
startswith()Code:
endswith()Code:
slicingCode:
web_name = '""The Finxter Acadcemy""' if web_name.startswith('"') and web_name.endswith('"'): web_name = web_name[1:-1]
print(web_name)The first line in the above code snippet declares a string containing two (2) sets of double quotes and saves this to the variable
Code:
web_nameThe following line calls the
Code:
ifCode:
startswith()Code:
endswith()Code:
'"'This statement checks to see if
Code:
web_nameCode:
slicingThe results are output to the terminal.
Code:
"The Finxter Acadcemy"Method 2: Use Regex
You can use the
Code:
regexCode:
re.sub()Code:
import re msg = '""Boost Your Python Skills""'
msg = re.sub(r'^"|"$', '', msg)
print(msg )The first line in the above code snippet imports the
Code:
reThe following line declares a string containing two (2) sets of double quotes and saves this to the variable
Code:
msgThe next line uses
Code:
re.sub()Code:
msgThe results are output to the terminal.
Code:
"Boost Your Python Skills"Another option is to use
Code:
re.sub()- the string to replace and
- the string to replace it with.
Code:
msg = '""Boost Your Python Skills""'
msg = re.sub('""', '"', msg)
print(msg)Code:
"Boost Your Python Skills"Method 3: Use replace()
This method uses https://blog.finxter.com/python-regex/
Code:
replace()Code:
mission = '""Boost Collective Intelligence""'
mission = mission.replace('""', '"')
print(mission)The first line in the above code snippet declares a string containing two (2) sets of double quotes and saves this to the variable
Code:
missionThe following appends the
Code:
replace()Code:
mission- the string to replace, and
- the string to replace it with.
The results save back to
Code:
missionCode:
"Boost Collective Intelligence"Method 4: Use list()
This method passes a list containing double quotes to the list function. This option differs from the other options as it removes all double quotes.
Code:
user_emails = [""'alice@acme.ca', 'doug@acme.ca', 'fran@acme.ca', 'stan@acme.ca'""]
user_emails = list(user_emails)
print(user_emails)The first line in the above code contains a list of Finxter user’s emails with double quotes at the beginning and the end.
This saves to
Code:
user_emailsThe following line uses a list and passes
Code:
user_emailsCode:
user_emailsCode:
['alice@acme.ca', 'doug@acme.ca', 'fran@acme.ca', 'stan@acme.ca']
Note: Converting a list containing double quotes to a list removes all double quotes.Method 5: Use Pandas
This method uses Pandas to remove all double quotes from a CSV file.
Contents of CSV file
Code:
Store,Category,Product,Number""Toronto"",""Jeans"",""10534"",""15""""Montreal"",""Tops"",""5415"",""32""""Ottawa"",""Coats"",""98341"",""22""Code:
import pandas as pd df = pd.read_csv('quotes.csv',header=None)
df.replace('"', '', inplace=True, regex=True)
print(df)The above example imports the Pandas library. This library allows access to and manipulation of a Pandas DataFrame.
The following line reads in the CSV file, without the header row into the DataFrame,
Code:
dfIf output to the terminal, the DataFrame appears as follows:
|
0 |
1 |
2 |
3
0 |
Shop |
Category |
Product |
Number
1 |
Toronto”” |
Jeans”” |
10534″” |
15″”
2 |
Montreal”” |
Tops”” |
5415″” |
32″”
3 |
Ottawa”” |
Coats”” |
98341″” |
22″”
Note: By default, when the CSV file was imported, the beginning double quotes were removed, thus leaving the trailing double quotes.Next, all occurrences of trailing quotes are replaced (removed) from the DataFrame.
The output of the modified DataFrame,
Code:
df|
0 |
1 |
2 |
3
0 |
Shop |
Category |
Product |
Number
1 |
Toronto |
Jeans |
10534 |
15
2 |
Montreal |
Tops |
5415 |
32
3 |
Ottawa |
Coats |
98341 |
22
Summary
This article has provided five (5) ways to remove one set of double quotes from a string and all double quotes to select the best fit for your coding requirements.
Good Luck & Happy Coding!
Programmer Humor – Blockchain
[i]“Blockchains are like grappling hooks, in that it’s extremely cool when you encounter a problem for which they’re the right solution, but it happens way too rarely in real life.” source – xkcd[/i]

