Remove quotes from String in Python
Just use string methods .replace()
if they occur throughout, or .strip()
if they only occur at the start and/or finish:
a = '"sajdkasjdsak" "asdasdasds"'
a = a.replace('"', '')
'sajdkasjdsak asdasdasds'
# or, if they only occur at start and end...
a = a.strip('\"')
'sajdkasjdsak" "asdasdasds'
# or, if they only occur at start...
a = a.lstrip('\"')
# or, if they only occur at end...
a = a.rstrip('\"')
Remove all quotations within a string
Strings are immutable in python, string.replace
can't mutate an existing string so you need to do
string = """Hi" how "are "you"""
new_string = string.replace('"',"")
print(new_string)
Or reassign the existing variable with the new value
How do I strip quotes out of a list of strings?
Simple solution is use the str.replace method twice, to ensure that all types of the quotations are removed.
list1 = ['"22.23.24.25"', "'11.22.33.44'"]
list1 = [x.replace('"', '').replace("'", '') for x in list1]
print(list1)
['22.23.24.25', '11.22.33.44']
Remove quotes around specific string with ReGex Python module?
Replace the string surrounded by quotes with just the string. You can use a capture group in the regexp to get the string between the quotes.
text = '"Service Name" - "Users Connected: 359" - "Firstseen: 591230-EF"'
phrase = 'Firstseen: 591230-EF'
new_text = re.sub(f'"({phrase})"', r'\1', text)
print(new_text)
Output:
"Service Name" - "Users Connected: 359" - Firstseen: 591230-EF
If you want to match something other than a fixed string, put that into phrase
. For instance, you can match two different fixed strings with alternation:
phrase = 'Part 3|Data 4'
If you want to match any code after Firstseen
, it would be
phrase = r'Firstseen: \d+-[A-Z]+'
removing quotes and double quotes from a list of words
Real texts may contains too many tricky symbols: n-dash –
, m-dash —
, over ten different quotes " ' ` ‘ ’ “ ” « » ‹› et cetera, et cetera...
It makes little sense to try to count all the possible punctuation symbols. Common way is try to get only letters (and spaces). Easiest way is to use RegExp:
import re
text = '''“Why, my dear, you must know, Mrs. Long says that Netherfield is
taken by a young man of large fortune from the north of England;
that he came down on Monday in a chaise and four to see the
place, and was so much delighted with it that he agreed with Mr.
Morris immediately; that he is to take possession before
Michaelmas, and some of his servants are to be in the house by
the end of next week.”
“What is his name?”
“Bingley.”
“Is he married or single?”
“Oh! single, my dear, to be sure! A single man of large fortune;
four or five thousand a year. What a fine thing for our girls!”
“How so? how can it affect them?”
“My dear Mr. Bennet,” replied his wife, “how can you be so
tiresome! You must know that I am thinking of his marrying one of
them.”
“Is that his design in settling here?”'''
# remove everything except letters, spaces, \n and, for example, dashes
text = re.sub("[^A-z \n\-]", "", text)
# split the text by spaces and \n
output = text.split()
print(output)
But actually the matter is much more complicated than it looks at first glance. Say I'm
is a two words? Probably so. What about someone's
? Or rock'n'roll
.
How to remove quotes from list of strings and store it as list again..?
You can use the built in python exec() function that will execute any string as code.
#!/usr/bin/env python3
fruits = ['apple','mango','orange']
def apple():
print("In apple")
def mango():
print("In mango")
def orange():
print("In orange")
for func in fruits:
exec(func + '()')
Output
In apple
In mango
In orange
Related Topics
How to Use Qscrollarea to Make Scrollbars Appear
Filling in Login Forms in Instagram Using Selenium and Webdriver (Chrome) Python Osx
Jupyter Notebook with Python 3.8 - Notimplementederror
Separation of Business Logic and Data Access in Django
How to Remove Packages Installed with Python's Easy_Install
Peak-Finding Algorithm for Python/Scipy
Convert a List of Characters into a String
Is a Python Dictionary an Example of a Hash Table
Determine If Python Is Running Inside Virtualenv
How to Implement Server Push in Flask Framework
What's the Difference Between %S and %D in Python String Formatting
Numpy Index Slice Without Losing Dimension Information
Pandas/Python: Set Value of One Column Based on Value in Another Column
How to Escape Special Characters of a String with Single Backslashes
Python Pandas Max Value in a Group as a New Column
Find Column Name in Pandas That Matches an Array
Why in Numpy 'Nan == Nan' Is False While Nan in [Nan] Is True