简体   繁体   中英

How to make string when a sentence has quotation marks or inverted commas? Python

For example I have a sentence such as:

Jamie's car broke "down" in the middle of the street

How can I convert this into a string without manually removing the quotation marks and inverted commas like:

'Jamies car broke down in the middle of the street' 

Any help is appreciated! Thanks,

Use replace() one after other:

s = """Jamie's car broke "down" in the middle of the street"""

print(s.replace('\'', '').replace('"', ''))
# Jamies car broke down in the middle of the street

You may use regex to remove all special characters from string as:

>>> import re
>>> my_str = """Jamie's car broke "down" in the middle of the street"""

>>> re.sub('[^A-Za-z0-9\s]+', '', my_str)
'Jamies car broke down in the middle of the street'

Try this:

oldstr = """Jamie's car broke "down" in the middle of the street""" #Your problem string
newstr = oldstr.replace('\'', '').replace('"', '')) #New string using replace()
print(newstr) #print result

This returns:

Jamies car broke down in the middle of the street

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM