替换字符串中的单引号和双引号

时间:2020-04-10 14:13:05

标签: python python-3.x string parsing

我正在尝试学习python,但遇到一个问题,即遇到需要替换单引号和双引号的字符串。我的目标是将它们拆分为一个列表,但出于其他原因仍保留单引号和双引号。我尝试了以下操作,但收到错误消息

s=("I said, "hello" it's mine".replace(""",'`')).replace("'","^")
print(s)

File "<ipython-input-58-a5276888c42f>", line 1
    s=("I said, "hello" it's mine".replace(""",'`')).replace("'","^")
                     ^
SyntaxError: invalid syntax

1 个答案:

答案 0 :(得分:0)

只需使用反斜杠即可将双引号转义。

s = "I said, \"hello\" it's mine"

或使用单引号引起来的单引号。 (这是规范的表示形式。)

s = 'I said, "hello" it\'s mine'

或使用三引号-单引号或双引号。

s = """I said, "hello" it's mine"""
s = '''I said, "hello" it's mine'''
相关问题