当句子有引号或引号时,如何制作字符串?蟒蛇

问题描述 投票:0回答:3

例如,我有一句话如下:

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

如何将其转换为字符串而无需手动删除引号和引号,如:

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

任何帮助表示赞赏!谢谢,

python string python-3.x converters
3个回答
2
投票

一个接一个地使用replace()

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

1
投票

您可以使用正则表达式从字符串中删除所有特殊字符:

>>> 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'

1
投票

试试这个:

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

返回:

Jamies car broke down in the middle of the street
© www.soinside.com 2019 - 2024. All rights reserved.